In our production environment we are running drop cache command echo 3 > /proc/sys/vm/drop_caches
to free the RAM. But also what I found is dropping caches is not a good practice and also it won't help much because when a process require more memory and if that much of memory is not free then OS will clear the cache and allocate memory accordingly.
Also technically value of available memory before and after running drop_caches
command should be same.
But when I ran few tests found that dropping caches really helped me in getting some free RAM
free -h
- Before dropping cache
total used free shared buff/cache available
Mem: 7.6G 4.2G 524M 306M 2.9G 2.8G
Swap: 4.0G 439M 3.6G
free-h
- After dropping cache
total used free shared buff/cache available
Mem: 7.6G 3.3G 3.8G 306M 452M 3.8G
Swap: 4.0G 439M 3.6G
As we can see available memory increased by 1GB, what more interesting is used memory almost reduced by 1GB. This is where I'm very much confused, isn't drop_cache
command should have only cleared cached memory
? why it is even clearing memory used by some running process(i.e used memory)
Can someone please explain
- how
echo 3 > /proc/sys/vm/drop_caches
command is working ? - how available memory is calculated in free command (breakdown of available memory)
After edit
One of my questions is still not answered
Also technically value of available memory before and after running drop_caches command should be same
As we dicussed, available memory includes reclaimable slab objects
Right, since reclaimable slab objects isn’t included in either free, buffers, or cache, that means that it ends up in used. The definition of “available” explicitly includes it so it is included there too.
But when I ran echo 2 > /proc/sys/vm/drop_caches
- To free reclaimable slab objects (includes dentries and inodes)
Before dropping cache
total used free shared buff/cache available
Mem: 7.6G 4.2G 397M 331M 3.0G 2.7G
Swap: 4.0G 429M 3.6G
After dropping cache
total used free shared buff/cache available
Mem: 7.6G 3.3G 3.8G 331M 483M 3.8G
Swap: 4.0G 429M 3.6G
As we can see here available memory has increased after dropping cache, but since I've used echo 2
, it only free reclaimable slab objects
. As available memory always includes reclaimable slab objects
running echo 2 > /proc/sys/vm/drop_caches
shouldn't have changed value of available memory.
But it is not true in my case.
echo 3 > /proc/sys/vm/drop_caches
is working as expected, it’s the calculation for “used” that doesn’t match your expectations; see the linked question for details (and the links in the answer there for details of “available”). – Stephen Kitt Dec 05 '22 at 16:15drop_caches
clears cached memory, how does the value of used memory is getting reduced after runningdrop_caches
. I'm not able to find the link here – Swastik Dec 05 '22 at 16:34