Linux

Linux Memory Management: Diagnostics and Optimization

This guide teaches you how to diagnose RAM shortages in Linux and apply proven optimization methods, from swap configuration to cache and process management.

Updated at February 14, 2026
20-30 min
Medium
FixPedia Team
Применимо к:Ubuntu 22.04+Debian 11+CentOS 8+RHEL 9+Fedora 36+

Diagnosing Memory Shortages in Linux

Before optimizing, you need to understand exactly how your system uses memory. Linux actively caches data in free RAM to speed up operations, so the "free" (free) value in the output of the free -h command is often low—this is normal.

Key Commands for Analysis

# Main information about RAM and swap usage
free -h

# Dynamic monitoring sorted by memory
top
# In top: press 'M' to sort by memory, 'P' — by CPU

# Statistics on virtual memory and disk activity
vmstat 1 5

# List of processes sorted by memory consumption
ps aux --sort=-%mem | head -10

# Detailed information for a specific process (replace <PID>)
cat /proc/<PID>/status | grep -E "Vm(RSS|Swap|Size)"

What to look for:

  • The Mem: line in free. The available field shows memory available for new applications without swapping.
  • si/so in vmstat (swap in/out). Consistently non-zero values indicate active swap usage—a sign of insufficient RAM.
  • High %MEM for specific processes in top or ps.

Method 1: Configuring the swappiness Parameter

vm.swappiness controls the kernel's tendency to use swap. Values range from 0 to 100.

  • 0: The kernel will avoid swap until physical RAM and cache are exhausted.
  • 60 (default): Aggressive swap usage.
  • 100: Active eviction of inactive pages to swap.

Recommendations:

  • Servers (databases, web servers): 10-20. Minimizes latency.
  • Desktop systems: 10-15. Ensures responsiveness.
  • Systems with hibernation: Set the value to 100 or close to it to guarantee space for saving state to swap.

How to change temporarily (until reboot):

sudo sysctl vm.swappiness=10

How to change permanently:

  1. Edit /etc/sysctl.conf:
    sudo nano /etc/sysctl.conf
    
  2. Add the line:
    vm.swappiness=10
    
  3. Apply the changes:
    sudo sysctl -p
    

Method 2: Managing Swap Space

Checking Existing Swap Areas

swapon --show
cat /proc/swaps

Creating a Swap File (if swap is insufficient or absent)

# 1. Create a file of the desired size (e.g., 2 GB)
sudo fallocate -l 2G /swapfile

# 2. Set correct permissions
sudo chmod 600 /swapfile

# 3. Create the swap area
sudo mkswap /swapfile

# 4. Enable the swap file immediately
sudo swapon /swapfile

# 5. Add to /etc/fstab for mounting at boot
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Removing an Unneeded Swap File

sudo swapoff /swapfile
sudo rm /swapfile
# Remove the corresponding line from /etc/fstab

⚠️ Important: Using swap on an SSD reduces its lifespan. For servers with large amounts of RAM (32+ GB), swap is often unnecessary.

Method 3: Clearing the File Cache (Pagecache, Dentries, Inodes)

The Linux kernel automatically manages the cache, freeing memory for new processes. However, in rare cases (e.g., after deleting millions of small files), forced clearing can help free memory immediately.

Clearing levels:

  1. 1 — Clear only pagecache.
  2. 2 — Clear pagecache and dentries.
  3. 3 — Clear pagecache, dentries, and inodes (most thorough).

Command to clear (level 3):

# First, sync data to disk just in case
sync
# Clear all cache types
echo 3 | sudo tee /proc/sys/vm/drop_caches

Check the result:

free -h

The cached field should decrease significantly, while available should increase.

💡 Tip: Perform clearing only during periods of low load (night, weekends). Do not make this a regular maintenance task.

Method 4: Managing Processes and Their Memory

If a specific process is "eating" memory, consider the following actions:

  1. Restart the service: Often helps free memory leaks.
    sudo systemctl restart <service_name>
    
  2. Limit memory via cgroups: For untrusted or unpredictable processes.
    # Create a cgroup and set a limit (e.g., 500M)
    sudo systemd-run --scope -p MemoryMax=500M <command>
    
  3. Use ulimit:
    # Set a memory size limit for the current session
    ulimit -v 500000  # in kilobytes
    
  4. Terminate the problematic process:
    # Find the PID and terminate (be careful!)
    sudo kill -9 <PID>
    

Method 5: Analyzing and Configuring OOM Killer

If the system has completely exhausted RAM and swap, the kernel activates OOM (Out-Of-Memory) Killer, which forcibly terminates processes to free memory.

  1. Check OOM Killer logs:
    sudo grep -i 'killed process' /var/log/syslog
    sudo journalctl -xe | grep -i oom
    

    The log will show which process was killed and its oom_score.
  2. Adjust oom_score_adj for critical processes: Values range from -1000 (maximum protection) to +1000 (maximum priority for killing).
    # Set a low kill priority for Nginx
    sudo systemctl set-property --runtime nginx.service OOMScoreAdjust=-900
    
    # For an already running process (by PID)
    echo -900 | sudo tee /proc/<PID>/oom_score_adj
    

    To make changes persistent after reboot, use systemctl set-property without --runtime or configure it in the systemd unit.

Method 6: Prevention and Long-Term Solutions

  1. Regular monitoring: Set up alerts in Prometheus/Grafana, Zabbix, or even a simple script with cron and mail.
    # Example script to check if available < 10%
    # (save as /usr/local/bin/check_mem.sh)
    if [ $(free | awk '/Mem:/ {print $7}') -lt $(free | awk '/Mem:/ {print $2}') * 0.1 ]; then
        echo "ALERT: Low memory on $(hostname)" | mail -s "Memory Alert" admin@example.com
    fi
    
  2. RAM upgrade: The most effective and reliable solution. Estimate peak usage over several weeks (free -h during maximum load) and add a buffer.
  3. Application optimization:
    • For Java applications: configure -Xmx (maximum heap) according to available RAM.
    • For web servers (Nginx/Apache): configure limits on the number of worker processes (worker_processes, max_connections).
    • For caches (Redis, Memcached): set maxmemory and an eviction policy (allkeys-lru, volatile-lru).

Summary Table of Actions

SymptomFirst ActionNext Steps
Slow performance, si/so > 0 in vmstatCheck free -h, topAdjust swappiness, add/increase swap, find memory-hogging processes.
High cached in free, but applications are sluggishUsually normal. Check available.Do not touch cache. Look for causes in I/O or CPU.
Regular "Killed process" messages in logsCheck OOM logs.Adjust oom_score_adj for critical services, add RAM.
Free RAM is available, but an application cannot allocate memoryCheck limits (ulimit, cgroups).Increase limits, check for memory leaks in the application.

Final Recommendations

Memory management in Linux is a balance between the kernel's automatic mechanisms and manual tuning. Start with diagnostics (free, top, vmstat), then:

  1. For most desktops and servers, setting vm.swappiness to 10-20 is sufficient.
  2. A swap file is a simple way to add a "buffer" without repartitioning disks.
  3. Cache clearing is an extreme measure, not a routine operation.
  4. If the problem lies with a specific application, configure its limits or consider replacing/optimizing it.
  5. Constant memory shortage is a signal to increase physical RAM.

Remember: Linux uses free RAM for disk caching, which improves overall performance. The goal is not to have 100% "free" RAM, but to have sufficient available memory for new processes and to avoid active swap usage.

F.A.Q.

What's the difference between RAM and swap in Linux?
Is it safe to manually clear memory cache?
What's the optimal swap size for a server?
Why is the system slower after clearing the cache?

Hints

Check current memory usage
Analyze memory-consuming processes
Adjust swappiness parameter
Optimize or add swap
Clear filesystem cache (if necessary)
Configure OOM Killer or add RAM
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