How to clear cache, buffer in linux (Ubuntu 16.04)

For running any application on any operating system requires memory. Every operating system has its own technique for memory management. But still many times our system faced low memory issue and because of that system performance is degrade. This is because cache memory(RAM) is full. Cache memory is used to store data which is frequently used by operating system. To clear cache most common way is Reboot the system but this is not a good solution. In linux we can clear cache or buffer manually.

Lets first check how much memory is used by our system using free -m command:
# free -m
              total        used        free      shared  buff/cache   available
Mem:          2873        2240          81         221         550         210
Swap:          1905         178        1727
Check in details memory usage using meminfo command:
# cat /proc/meminfo
MemTotal:        2942168 kB
MemFree:          274208 kB
MemAvailable:     508524 kB
Buffers:           45664 kB
Cached:           535960 kB
SwapCached:         6044 kB
Active:          1794220 kB
Inactive:         692760 kB
Active(anon):    1651944 kB
Inactive(anon):   492360 kB
Active(file):     142276 kB
Inactive(file):   200400 kB
Unevictable:       10348 kB
Mlocked:           10348 kB
SwapTotal:       1951740 kB
SwapFree:        1769120 kB
Dirty:               248 kB
There are so many entries.
There are three drop_cache options to clear cache memory:
1) free PageCache only
sync; echo 1 > /proc/sys/vm/drop_caches
2) free Dentries and Inodes
sync; echo 2 > /proc/sys/vm/drop_caches
3) cree PageCache, Dentries and Inodes
sync; echo 3 > /proc/sys/vm/drop_caches
sync will flush the file system buffer.drop_caches delete the cached objects. If you use this command while running application it may cause performance issue.Its bad idea to run drop_cache command when your server is under heavy usage.
now check memeory usage:
free -m
              total        used        free      shared  buff/cache   available
Mem:           2873        1917        590         193      365         578
Swap:          1905         178        1727
Thats all this is the simple way to clear cache.


Post a Comment