How to Free Linux Memory Cache and Understand Buffer vs Cache
This guide explains Linux memory reporting with the free command, the roles of buffer and page caches, and provides step‑by‑step instructions to manually release cached memory using /proc/sys/vm/drop_caches, including practical command examples and clarifications of key concepts.
Understanding Memory Reporting with free
The free command shows total, used, free, shared, buffers, and cached memory. In the example, total memory is shown, with used 163 MB, free 86 MB, buffers 10 MB, and cached 94 MB. After copying a file, used rises to 244 MB, free drops to 4 MB, buffers become 8 MB, and cached jumps to 174 MB, illustrating how the kernel uses cache to speed up I/O.
Why Linux Uses Buffer and Page Caches
Linux employs two main cache mechanisms: Buffer Cache for block‑device I/O and Page Cache for file‑inode I/O. These caches reduce the time of system calls such as read, write, and getdents, improving overall performance.
Manually Dropping Caches via /proc/sys/vm/drop_caches
The kernel exposes /proc/sys/vm/drop_caches to allow administrators to free cached memory. The default value is 0. To safely drop caches, first run sync to flush pending writes, then write a value to the file: echo 1 > /proc/sys/vm/drop_caches – frees page cache only. echo 2 > /proc/sys/vm/drop_caches – frees dentries and inodes. echo 3 > /proc/sys/vm/drop_caches – frees page cache, dentries, and inodes.
Example session:
# cat /proc/sys/vm/drop_caches
0
# sync
# echo 3 > /proc/sys/vm/drop_caches
# cat /proc/sys/vm/drop_caches
3After executing the above, free shows used memory reduced to 66 MB, free increased to 182 MB, buffers 0 MB, and cached 11 MB, confirming that the caches were released.
Buffer vs. Cache – Core Differences
A buffer holds data that has not yet been written to disk, acting as a staging area between fast and slow devices. A cache stores data that has already been read from disk, enabling faster subsequent accesses.
In Linux terminology, buffers refer to the block‑device buffer cache, while cached refers to the page cache used by the filesystem.
Large cache values indicate many files are cached, reducing disk I/O. Buffers improve write efficiency and reduce device contention.
Additional Tips
Adjust vm.swappiness in /etc/sysctl.conf to control swap usage (range 0‑100, default 60).
The kernel periodically cleans buffers; manual sync can be used to ensure data is flushed before dropping caches.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
