Understanding Disk Cache in Linux

|
| By Webner

Understanding Disk Cache in Linux

Disk cache allocates memory (RAM) used by operating system to increase the performance, like in Linux and in windows. Sometimes when you check RAM utilization in Linux OS using the below command

Shell #> free -m
Or
Shell #> top

you may find free memory is very less and Disk Cache has occupied memory. Disk caching is responsible for the system to perform faster. There are no downsides except for the confusion. It can be a confusing thought that disk caching has occupied RAM of your system. If your system requires more memory to run any of your applications the system itself adjust and takes back the memory from disk cache. System OS is designed in such a way Disk cache can always be given back to applications immediately whenever needed.

How can we stop Linux to perform disk caching?
You cannot disable disk caching.

Many of us think that disk caching has occupied too much memory and the system might become unresponsive and we start trying to disable disk caching, but it is not like that. Disk cache makes applications load faster and run smoother, but it never blocks memory permanently. Therefore, there’s absolutely no reason to disable it.

Above is elaborated below:

How do you count memory that is currently used for something, but can still be made available to applications?

You might count that memory as “free” and/or “available”. Linux instead counts it as “used”, but also “available”:

Linux

This is just a difference in terminology. Both you and Linux agree that memory taken by applications is termed as “used”, while memory that is not used for anything is termed as “free”.

How can we check how much free RAM is available to use?
Below-mentioned command is used to show exact memory details in Linux OS:

shell> free -am

Linux

A healthy Linux system will show the following expected and harmless behavior:
When free memory is close to 0
When used memory is close to total
When available memory (or “free + buffers/cache”) has enough room (20%+ of total)
Or when swap used does not change

Alarming situation of a genuine low memory situation will show following behavior:
When available memory (or “free + buffers/cache”) is close to zero
When swap used increases or fluctuates
And if, dmesg | grep oom-killer shows the OutOfMemory-killer at work

Note : There is a command which you can use to forcibly remove cached data.
Shell #> echo 3 > /proc/sys/vm/drop_caches

Above command will clear PageCache, dentries and inodes.

1. Clear PageCache only.
# sync; echo 1 > /proc/sys/vm/drop_caches

2. Clear dentries and inodes.
# sync; echo 2 > /proc/sys/vm/drop_caches

3. Clear PageCache, dentries and inodes.
# sync; echo 3 > /proc/sys/vm/drop_caches

Leave a Reply

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