As part of doing some cold cache timings, I'm trying to free the OS cache. The kernel documentation (retrieved January 2019) says:
drop_caches
Writing to this will cause the kernel to drop clean caches, as well as
reclaimable slab objects like dentries and inodes. Once dropped, their
memory becomes free.
To free pagecache:
echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
echo 3 > /proc/sys/vm/drop_caches
This is a non-destructive operation and will not free any dirty objects.
To increase the number of objects freed by this operation, the user may run
`sync' prior to writing to /proc/sys/vm/drop_caches. This will minimize the
number of dirty objects on the system and create more candidates to be
dropped.
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.
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.
You may see informational messages in your kernel log when this file is
used:
cat (1234): drop_caches: 3
These are informational only. They do not mean that anything is wrong
with your system. To disable them, echo 4 (bit 3) into drop_caches.
I'm a bit sketchy about the details. Running
echo 3 > /proc/sys/vm/drop_caches
frees pagecache, dentries and inodes. Ok.
So, if I want the system to start caching normally again, do I need to reset it to 0 first? My system has the value currently set to 0, which I assume is the default. Or will it reset on its own? I see at least two possibilities here, and I'm not sure which one is true:
echo 3 > /proc/sys/vm/drop_caches
frees pagecache, dentries and inodes. The system then immediately starts caching again. I'm not sure what I would expect the value in/proc/sys/vm/drop_caches
to do if this is the case. Go back to 0 almost immediately?If
/proc/sys/vm/drop_caches
is set to 3, the system does not do any memory caching till it is reset to 0.
Which case is true?
echo 3 | sudo tee /proc/sys/vm/drop_caches
– Volker Siegel Aug 03 '14 at 22:40sudo -i && echo 3 > /proc/sys/vm/drop_caches
, I get an error:stdin: is not a tty
. Using your "sudo" method works. Why? – harperville Feb 26 '16 at 15:57/proc/sys/vm/drop_caches
on your local system, which means you'll (if successful, no idea how it behaves with arbitrary input) drop caches on the host you're SSHing from, if at all. Inputting sudo password over SSH is a bit more involved, but doable. – Legogris Nov 13 '20 at 15:44sync && sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"
– kaliko Feb 04 '21 at 08:25