What does drop_caches do?

Greg Freemyer greg.freemyer at gmail.com
Wed Aug 24 03:18:25 EDT 2011


On Thu, Jul 28, 2011 at 2:58 AM, Prateek Sharma <prateeks at cse.iitb.ac.in> wrote:
> Hello everyone,
>        I've been trying to understand the role of the pagecache, starting with
> drop_caches and observing what it does.
>        From my understanding of the code (fs/drop_caches.c) , it walks over all
> the open files/inodes, and invalidates all the mapped pages.

Stepping up a little, I think you're missing
drop_caches_sysctl_handler() in that same file.  It is the entry point
I believe that implements the userspace ABI defined in
Documentation/sysctl/vm.txt.

==============================================================
 139
 140drop_caches
 141
 142Writing to this will cause the kernel to drop clean caches, dentries and
 143inodes from memory, causing that memory to become free.
 144
 145To free pagecache:
 146        echo 1 > /proc/sys/vm/drop_caches
 147To free dentries and inodes:
 148        echo 2 > /proc/sys/vm/drop_caches
 149To free pagecache, dentries and inodes:
 150        echo 3 > /proc/sys/vm/drop_caches
 151
 152As this is a non-destructive operation and dirty objects are not
freeable, the
 153user should run `sync' first.
 154
 155==============================================================

Note the corresponding logic in /fs/drop_caches.c that implements the
above binary logic.


  61              if (sysctl_drop_caches & 1)
  62                        iterate_supers(drop_pagecache_sb, NULL);
  63                if (sysctl_drop_caches & 2)
  64                        drop_slab();

Greg



More information about the Kernelnewbies mailing list