Clear Cached Memory On Linux Without Reboot

Clear Cached Memory On Linux Without Reboot

If a Linux server appears to have inodes or dentries filling up cached memory, here is how you can flush it without putting the server through a reboot.

This will only work with linux kernels equal to or greater then version 2.6.16+ which was introduced around late 2005.

1 ) Check available, used, cached memory with this command:

[root@localhost]# top -n1 | grep -i "used," ; free -t -m
Mem:   4035004k total,  1478408k used,  2556596k free,   225932k buffers
Swap:  6088624k total,      156k used,  6088468k free,   675708k cached
             total       used       free     shared    buffers     cached
Mem:          3940       1443       2496          0        220        659
-/+ buffers/cache:        563       3377
Swap:         5945          0       5945
Total:        9886       1443       8442

In this case, approximately 659MB is cached, in most cases it can reach gigabyte ranges on production servers. Follow step #2.

2 ) Commit any buffers to disk first with following command:

sync

root@localhost]# sync
[root@localhost]# 

3 ) Next Let's send signal now to kernel to flush pagecaches, inodes, and dentries:

echo 3 > /proc/sys/vm/drop_caches

[root@localhost]# echo 3 > /proc/sys/vm/drop_caches
[root@localhost]# 

4 ) Check system RAM again

[root@localhost]# top -n1 | grep -i "used," ; free -t -m
Mem:   4035004k total,   563996k used,  3471008k free,     1224k buffers
Swap:  6088624k total,      156k used,  6088468k free,    35912k cached
             total       used       free     shared    buffers     cached
Mem:          3940        550       3389          0          1         35
-/+ buffers/cache:        514       3425
Swap:         5945          0       5945
Total:        9886        550       9335

We have now cleared cached memory and freed up several hundred MB without a server reboot.