Linux

Linux Memory Optimization: Improving System Performance

This guide helps you reduce memory consumption, configure swap, and adjust Linux kernel parameters. Learn to control RAM usage and boost system performance without hardware upgrades.

Updated at February 17, 2026
15-30 min
Medium
FixPedia Team
Применимо к:Ubuntu 20.04/22.04Debian 11/12CentOS 7/8Fedora 35+Arch LinuxAny distribution with systemd and kernel 4.x+

Introduction / Why This Matters

RAM is a critical resource in Linux systems. Its shortage leads to heavy swap usage, which drastically reduces performance, especially on SSDs. This guide will help you optimize RAM usage by tuning kernel parameters and tools like zram. After completing it, you'll have a more responsive system, reduced disk load, and prevented sudden freezes due to memory shortage.

Requirements / Preparation

Before starting, ensure:

  • You have terminal access with sudo privileges.
  • Basic utilities are installed: free, vmstat, sysctl (included in most distributions).
  • Recommended: Back up /etc/sysctl.conf (sudo cp /etc/sysctl.conf /etc/sysctl.conf.bak) to revert changes if something goes wrong.

Step 1: Check Current Memory Status

First, understand how your system currently uses memory. Run:

free -h

Example output:

              total    used    free  shared  buff/cache   available
Mem:           7,7G    3,2G    1,1G    200M        3,4G        4,0G
Swap:          2,0G    512M    1,5G

Note:

  • used — actively used RAM.
  • buff/cache — memory used by cache (can be reclaimed).
  • available — estimate of memory available for new processes without swapping.
  • Swap — swap partition/file usage.

Additionally, run vmstat 1 5 (updates every second, 5 times). Key columns:

  • si (swap-in) — pages swapped into RAM from swap.
  • so (swap-out) — pages swapped out from RAM to swap. If si/so are consistently > 0, the system is actively using swap, indicating RAM shortage.

Step 2: Configure swappiness

The vm.swappiness parameter controls the kernel's tendency to use swap (range 0–100). 0 means swap is used only when absolutely necessary, 100 means aggressive swapping. For desktops/servers with sufficient RAM, 10–20 is recommended.

# Check current value
cat /proc/sys/vm/swappiness
# Set temporary value (e.g., 10)
sudo sysctl vm.swappiness=10

To make the change persistent after reboot, add to /etc/sysctl.conf:

vm.swappiness=10

Apply:

sudo sysctl -p

⚠️ Important: Do not set swappiness=0 on high-load servers — this can lead to OOM kills of processes during peak loads.

Step 3: Clear Memory Cache (If Needed)

The cache (page cache and dentries) is automatically managed by the kernel, but sometimes (e.g., after running a heavy application) you can clear it temporarily to free RAM. Do not do this regularly!

# Clear page cache (value 1)
echo 1 | sudo tee /proc/sys/vm/drop_caches
# Full clear (page cache + dentries + inodes)
echo 3 | sudo tee /proc/sys/vm/drop_caches

After clearing, the system may slow down temporarily as files are reloaded from disk.

Step 4: Configure vfs_cache_pressure

The vm.vfs_cache_pressure parameter controls how aggressively the kernel reclaims memory used by filesystem metadata cache (inode/dentry). Increasing the value (to 200–500) speeds up memory freeing when memory is low.

# Set value to 200
sudo sysctl vm.vfs_cache_pressure=200
# Save to /etc/sysctl.conf
echo "vm.vfs_cache_pressure=200" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Zram creates a compressed block device in RAM used as swap. It's faster than traditional disk swap and reduces SSD wear. Ideal for systems without a swap partition or with small RAM.

Installation and setup:

For Debian/Ubuntu:

sudo apt update && sudo apt install zram-tools

For Fedora/CentOS (after enabling repository):

sudo dnf install zram

For Arch:

sudo pacman -S zram

Configure the size in the configuration file (usually /etc/default/zramswap or /etc/modprobe.d/zram.conf). Example for 50% of RAM:

# In /etc/default/zramswap (Debian/Ubuntu)
PERCENTAGE=50

Start the service:

sudo systemctl enable --now zramswap
# Or manually (if no systemd unit)
sudo modprobe zram
echo lz4 | sudo tee /sys/block/zram0/comp_algorithm
echo $(( $(grep MemTotal /proc/meminfo | awk '{print $2}') * 512 / 1024 / 1024 * 50 / 100 )) | sudo tee /sys/block/zram0/disksize
sudo mkswap /dev/zram0
sudo swapon -p 10 /dev/zram0

Verify: swapon --show should display zram0.

Verify Results

  1. Compare free -h before and after settings. Expect:
    • Increased available memory.
    • Reduced si/so in vmstat (closer to 0).
    • If using zram — a device appears in swapon --show.
  2. Test system responsiveness:
    • Open several browser tabs or a heavy application.
    • Switch between them — switching delays should decrease.
  3. Real-time monitoring:
    watch -n 1 free -h
    

Potential Issues

1. Swap does not decrease after configuring swappiness

Cause: The kernel may not immediately apply new settings under load.
Solution: Reboot the system or wait for activity to decrease. Check that vm.swappiness changed via cat /proc/sys/vm/swappiness.

2. Zram does not activate after reboot

Cause: Service not enabled or module conflict.
Solution:

  • Ensure service is active: systemctl status zramswap.
  • Check kernel module: lsmod | grep zram. If missing — sudo modprobe zram.
  • For manual setup, add commands to /etc/rc.local or a systemd unit.

3. System hangs for 10–20 seconds after cache clearing

Cause: Waiting for files to reload from disk.
Solution: This is normal. Do not clear cache frequently. If it recurs — you likely have insufficient RAM for current load; consider upgrading or further optimization (e.g., reducing services).

4. Kernel parameters not applied after editing sysctl.conf

Cause: Syntax error in file or missing kernel module.
Solution:

  • Check syntax: sudo sysctl -p will show errors.
  • Ensure parameter is supported: sysctl -a | grep vm.swappiness. If not found — possibly an old kernel (< 3.5). Update kernel or use alternative methods (e.g., systemd units for zram).

F.A.Q.

Is it safe to change the swappiness parameter on a production server?
Does clearing memory cache actually help or is it harmful?
Which is better: increase disk swap or use zram?
How to know if memory optimization was successful?

Hints

Check current memory status
Configure swappiness
Clear cache (if necessary)
Configure vfs_cache_pressure
Implement zram (recommended for SSDs)

Did this article help you solve the problem?

FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community