When your Linux system is running low on memory, you can sometimes reclaim system memory by using the drop_caches command to free up memory that is being for caches.
View Free Linux System Memory Using Top or Free
You can see how much free memory your system has available by using either the top command and reviewing the header output:
# top top - 03:00:01 up 92 days, 20:32, 1 user, load average: 0.00, 0.01, 0.00 Tasks: 144 total, 1 running, 143 sleeping, 0 stopped, 0 zombie Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 1020584k total, 939344k used, 81240k free, 119084k buffers Swap: 1048568k total, 78232k used, 970336k free, 289940k cached
Or by using the free command:
# free -m total used free shared buffers cached Mem: 996 921 75 0 116 283 -/+ buffers/cache: 521 474 Swap: 1023 76 947
Note that using the “-m” option with the free command will display the results in MB. In the example above, We have only 75MB free, while we have 116 MB tied up in buffers and 283 MB cached. We can free up these cached and buffered MB by modifying the /proc/sys/vm/drop_caches file. We simply need to echo the proper number into the /proc/sys/vm/drop_caches file.
Clear or Free Linux Memory Using Drop_Caches
Echoing the following numbers into the /proc/sys/vm/drop_caches file will produce somewhat different results:
1 # free pagecache 2 # free dentries, inodes 3 # free pagecache, dentries, inodes
Here is a very short description of each of these options:
pagecache where the kernel stores page-sized bits of files used in normal operations.
dentries are caches specified for directory entries
inodes are “index nodes” that refer to files, directories, or symbolic links
Copying these numbers to the /proc/sys/vm/drop_caches file is a safe operation that will clear any unused cache of the type specified. By using the sync command, prior to this operation, we achieve the maximum memory recovery.
# sync # echo 3 > /proc/sys/vm/drop_caches
After clearing the caches, we can run the “free” command again to see the effect on the Linux system memory:
# free -m total used free shared buffers cached Mem: 996 563 432 0 0 54 -/+ buffers/cache: 508 487 Swap: 1023 76 947
Success!! We were able to clear up over 350MB of memory.
Remember That Linux Memory Caches are Good
Linux system memory caches are designed to improve system performance. By caching files in memory, less cpu and disk io is needed. By clearing memory caches in this manner, we are deferring more tasks to the processor and disks in an effort to clear up system memory. Be certain that you know what you are doing when performing this task.
Here are some additional resources on Linux System Memory that may help you:
Red Hat Documentation on Tuning Virtual Memory
Red Hat – Optimizing System Performance
Latest posts by Jeff Staten (see all)
- Configure Your HP Procurve Switch with SNTP - May 5, 2015
- Configuring HP Procurve 2920 Switches - May 1, 2015
- Troubleshooting Sendmail - November 28, 2014