Master Linux Memory: Decode free Output, Buffers, Cache, and Swap Cleanup
This guide explains how to interpret the Linux free command, distinguishes between used, free, buffers, and cache memory, describes common symptoms of memory pressure, and provides step‑by‑step commands to manually clear swap and drop caches for better system performance.
1. Viewing Memory with the free Command
The free command shows several columns:
total : total physical memory.
used : memory currently in use (includes cached, buffers, and shared memory).
free : truly idle memory.
shared : memory shared between processes (usually negligible).
buffers : data that has been written to memory but not yet flushed to disk.
cached : data read from disk that is kept in memory for faster future access.
Two derived values are often more useful:
-/+ buffers/cache (shown as -buffers/cache): memory actively used, calculated as used - buffers - cached.
+ buffers/cache : memory that can be reclaimed, calculated as free + buffers + cached.
2. Buffers vs. Cache
Cache is primarily for read‑heavy workloads; it stores recently accessed data so the CPU can retrieve it quickly without hitting the slower storage device.
Buffer is geared toward write operations, aggregating writes before they are flushed to disk, which reduces fragmentation and improves I/O performance. The kernel periodically writes buffered data to disk, and the sync command can trigger this manually.
3. Common Symptoms of Memory Pressure
Symptom 1 : Frequent file access quickly exhausts physical memory while the cached value keeps growing. This indicates that the system is caching data to keep the CPU fed.
Symptom 2 : Swap space becomes occupied. Swap usage signals that physical RAM is insufficient for current workloads.
4. Manually Clearing Swap and Buffers/Cache
(1) Clear Swap swapoff -a && swapon -a This command disables swap, forces any swapped‑out pages back into RAM, and then re‑enables swap. No data is lost as long as enough free memory (+ buffers/cache) is available.
(2) Drop Buffers and Cache
sync; sync; sync && echo 3 > /proc/sys/vm/drop_caches sleep 2 echo 0 > /proc/sys/vm/drop_cachesExplanation: sync writes dirty pages from memory to disk. echo 3 > /proc/sys/vm/drop_caches tells the kernel to drop page cache, dentries, and inodes (value 3). The default is 0. sleep 2 gives the system a moment to finish the previous operation. echo 0 > /proc/sys/vm/drop_caches restores the default setting.
5. Summary
When free memory appears low, it does not necessarily mean the system is under‑performing, because a large portion of memory may be occupied by buffers and cache that can be reclaimed instantly. Persistent swap usage and a non‑zero bi / bo (block I/O) are stronger indicators of genuine memory pressure. Monitoring the -/+ buffers/cache line from free gives a clearer picture of usable memory.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
