Can You Actually Free Linux Buffers and Cache? A Deep Dive into Memory Management
This article demystifies Linux memory management by explaining why buffers and cache are not meant to be manually cleared, how the 'available' metric differs from 'free', and which sysctl parameters and cgroup settings you should tune to optimize performance and avoid OOM incidents.
Linux treats unused memory as a resource to accelerate I/O, using lazy allocation for virtual memory and reclaim mechanisms such as page cache, swap, and OOM Killer. The free command shows total, used, free, shared, buff/cache, and available memory, but only the available column reflects memory that applications can actually allocate.
Key insight : available = free + reclaimable buff/cache. The kernel can instantly free reclaimable pages, so a low free value does not indicate pressure if available remains high.
Buffers vs. Cache
Buffer : metadata for block devices (e.g., superblock, inode). Typically small on modern systems because most I/O goes through the page cache.
Cache (Page Cache): cached file contents, directory entries, and inode data. It dominates memory usage and is fully reclaimable when needed.
Both are unified under the page‑cache framework since Linux 2.4, and /proc/meminfo provides the exact fields ( Buffers, Cached, SwapCached, etc.).
Correct usage calculation
# memory_used_percent = (total - available) / total * 100Older scripts that used (total - free) / total caused false alarms.
Practical commands
# Show memory with proper columns
$ free -h
# Inspect detailed fields
$ grep -E "(MemTotal|MemAvailable|Buffers|Cached)" /proc/meminfo
# Verify OOM score of a process
$ cat /proc/$(pidof mysqld)/oom_score
# Adjust OOM priority
$ echo -500 > /proc/$(pidof mysqld)/oom_score_adjSysctl tuning examples
# Reduce swap usage (recommended for databases)
vm.swappiness = 10
# Control dirty page thresholds
vm.dirty_ratio = 10
vm.dirty_background_ratio = 5
# Adjust VFS cache pressure
vm.vfs_cache_pressure = 50
# Set minimum free memory (example for 32 GB RAM)
vm.min_free_kbytes = 131072Apply the settings with sysctl -p or place them in /etc/sysctl.d/99-memory-tuning.conf.
cgroup memory limits
cgroups v1: memory.limit_in_bytes and memory.memsw.limit_in_bytes cgroups v2: memory.max (hard limit) and memory.high (soft limit)
systemd unit example:
[Service]
MemoryMax=4G
MemoryHigh=3G
OOMScoreAdjust=-500Swap strategy : Small swap with low swappiness for database servers; larger swap or disabled swap for latency‑sensitive workloads. Adjust vm.overcommit_memory to 1 for Redis to avoid fork failures.
Monitoring : Use node_exporter (collectors meminfo and vmstat) with Prometheus and Grafana. Example alert rule for high memory pressure:
# Prometheus alert
- alert: HighMemoryPressure
expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes > 0.85
for: 5m
labels:
severity: warning
annotations:
summary: "Memory available below 15%"Best practices
Never manually drop caches in production; let the kernel reclaim automatically.
Set swappiness between 10‑30 for typical servers; 0‑1 for memory‑intensive services.
Use oom_score_adj to protect critical processes.
Monitor node_memory_MemAvailable_bytes, page‑fault rates, and OOM events.
By understanding the true meaning of available memory, applying targeted sysctl tweaks, and configuring cgroup limits, you can avoid unnecessary alarms, improve application performance, and prevent unexpected OOM terminations.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
