Showing posts with label Cache memory in linux. Show all posts
Showing posts with label Cache memory in linux. Show all posts

Wednesday 23 December 2015

How to Clear Cache Memory in Linux

Find Memory Status

[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        915         71          7          0        395
-/+ buffers/cache:        519        467
Swap:         1023          0       1023

In this example the total amount of available memory is 987 M. 519 M are used by processes and 467 M are free for other applications. Do not get confused by the first line which shows that 71 M are free! If you look at the usage figures you can see that most of the memory use is for buffers and cache. Linux always tries to use RAM to speed up disk operations by using available memory for buffers (file system metadata) and cache (pages with actual contents of files or block devices). This helps the system to run faster because disk information is already in memory which saves I/O operations. If space is needed by programs or applications like Oracle, then Linux will free up the buffers and cache to yield memory for the applications. If your system runs for a while you will usually see a small number under the field "free" on the first line.


Find Cache Memory uses in Linux

Use free command to find out cache memory uses by Linux system. Output of free command is like below

Last column is showing cached memory (  395 MB) by system. -m option is used for showing memory details in MB’s.


[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        915         71          7          0        395
-/+ buffers/cache:        519        467
Swap:         1023          0       1023

Following command to see much detailed information about current memory usage in 3 second interval:

[root@localhost Desktop]# watch -n 3 cat /proc/meminfo

Every 3.0s: cat /proc/meminfo                                                          Wed Dec 23 23:28:53 2015

MemTotal:        1010884 kB
MemFree:          353384 kB
MemAvailable:     367148 kB
Buffers:              40 kB
Cached:           123052 kB
SwapCached:            0 kB
Active:           427352 kB
Inactive:          83868 kB
Active(anon):     385120 kB
Inactive(anon):    11240 kB
Active(file):   42232 kB
Inactive(file):    72628 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal: 1048572 kB
SwapFree:        1048572 kB
Dirty:                40 kB
Writeback:             0 kB
AnonPages:        388152 kB
Mapped:            95736 kB
Shmem:              8216 kB
Slab:              67208 kB


1. Freeing pagecache only use following command

[root@localhost Desktop]# sync; echo 1 > /proc/sys/vm/drop_caches


[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        611        375          7          0         92
-/+ buffers/cache:        518        468
Swap:         1023          0       1023

2. Freeing dentries and inodes use following command

[root@localhost Desktop]# sync; echo 2 > /proc/sys/vm/drop_caches


[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        594        392          7          0         93
-/+ buffers/cache:        501        485
Swap:         1023          0       1023

3. Freeing pagecache, dentries and inodes in cache memory

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


[root@localhost Desktop]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        596        391          7          0         91
-/+ buffers/cache:        504        482
Swap:         1023          0       1023
[root@localhost Desktop]#

4. Schedule Cron to Flush Cache Regularly

If we want to schedule following in crontab to automatically flush cache on regular interval.

# crontab -l

0 * * *  * sync; echo 3 > /proc/sys/vm/drop_caches

The above cron will execute on every hour and flushes the cached memory on system.