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 infree. Theavailablefield shows memory available for new applications without swapping. si/soinvmstat(swap in/out). Consistently non-zero values indicate active swap usage—a sign of insufficient RAM.- High
%MEMfor specific processes intoporps.
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
100or 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:
- Edit
/etc/sysctl.conf:sudo nano /etc/sysctl.conf - Add the line:
vm.swappiness=10 - 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— Clear only pagecache.2— Clear pagecache and dentries.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:
- Restart the service: Often helps free memory leaks.
sudo systemctl restart <service_name> - 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> - Use
ulimit:# Set a memory size limit for the current session ulimit -v 500000 # in kilobytes - 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.
- 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 itsoom_score. - Adjust
oom_score_adjfor 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, usesystemctl set-propertywithout--runtimeor configure it in the systemd unit.
Method 6: Prevention and Long-Term Solutions
- Regular monitoring: Set up alerts in Prometheus/Grafana, Zabbix, or even a simple script with
cronandmail.# 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 - RAM upgrade: The most effective and reliable solution. Estimate peak usage over several weeks (
free -hduring maximum load) and add a buffer. - 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
maxmemoryand an eviction policy (allkeys-lru,volatile-lru).
- For Java applications: configure
Summary Table of Actions
| Symptom | First Action | Next Steps |
|---|---|---|
Slow performance, si/so > 0 in vmstat | Check free -h, top | Adjust swappiness, add/increase swap, find memory-hogging processes. |
High cached in free, but applications are sluggish | Usually normal. Check available. | Do not touch cache. Look for causes in I/O or CPU. |
| Regular "Killed process" messages in logs | Check OOM logs. | Adjust oom_score_adj for critical services, add RAM. |
| Free RAM is available, but an application cannot allocate memory | Check 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:
- For most desktops and servers, setting
vm.swappinessto 10-20 is sufficient. - A swap file is a simple way to add a "buffer" without repartitioning disks.
- Cache clearing is an extreme measure, not a routine operation.
- If the problem lies with a specific application, configure its limits or consider replacing/optimizing it.
- 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.