0

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

  1. how echo 3 > /proc/sys/vm/drop_caches command is working ?
  2. 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:15
  • @StephenKitt does increase in available memory after running drop_cache is expected? – Swastik Dec 05 '22 at 16:24
  • @StephenKitt Also what I still don't understand is, if used memory excludes cached memory and drop_caches clears cached memory, how does the value of used memory is getting reduced after running drop_caches. I'm not able to find the link here – Swastik Dec 05 '22 at 16:34
  • 1
    You’re not just dropping caches, you’re reclaiming slab objects; that accounts for the increase in free memory. – Stephen Kitt Dec 05 '22 at 16:35
  • 1
    @StephenKitt then why dropping cache is not treated as good practice, when it can actually free some RAM? and why it is not much common? – Swastik Dec 05 '22 at 16:40
  • 1
    Because the kernel knows how to free reclaimable slab objects when necessary too. See the kernel docs on the topic: “This file is not a means to control the growth of the various kernel caches (inodes, dentries, pagecache, etc…) These objects are automatically reclaimed by the kernel when memory is needed elsewhere on the system.” – Stephen Kitt Dec 05 '22 at 16:42
  • “Use of this file can cause performance problems. Since it discards cached objects, it may cost a significant amount of I/O and CPU to recreate the dropped objects, especially if they were under heavy use. Because of this, use outside of a testing or debugging environment is not recommended.” – Stephen Kitt Dec 05 '22 at 16:42

0 Answers0