Free Up Linux Memory Using Drop_Caches

memory2When 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.

Additional Resources

Here are some additional resources on Linux System Memory that may help you:

Red Hat Documentation on Tuning Virtual Memory

https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Performance_Tuning_Guide/s-memory-tunables.html

 

Red Hat – Optimizing System Performance

https://access.redhat.com/site/documentation/en-US/Red_Hat_Directory_Server/8.2/html/Performance_Tuning_Guide/system-tuning.html

The following two tabs change content below.
Jeff has 20 years of professional IT experience, having done nearly everything in his roles of IT consultant, Systems Integrator, Systems Engineer, CNOC Engineer, Systems Administrator, Network Systems Administrator, and IT Director. If there is one thing he knows for sure, it is that there is always a simple answer to every IT problem and that downtime begins with complexity. Seasoned IT professional by day, Jeff hopes to help other IT professionals by blogging about his experiences at night on his blog: http://uptimemadeeasy.com. You can find Jeff on or LinkedIn at: LinkedIn or Twitter at: Twitter

Latest posts by Jeff Staten (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *