设置 /proc/sys/vm/drop_caches 以清除缓存

设置 /proc/sys/vm/drop_caches 以清除缓存

作为进行一些冷缓存计时的一部分,我正在尝试释放操作系统缓存。这内核文档(2019 年 1 月检索)说:

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.

我对细节有点粗略。跑步

echo 3 > /proc/sys/vm/drop_caches

释放页面缓存、目录项和索引节点。好的。

那么,如果我想让系统再次正常启动缓存,是否需要先将其重置为0呢?我的系统当前将值设置为 0,我认为这是默认值。还是会自行重置?我在这里至少看到两种可能性,但我不确定哪一种是正确的:

  1. echo 3 > /proc/sys/vm/drop_caches释放页面缓存、目录项和索引节点。然后系统立即再次开始缓存。/proc/sys/vm/drop_caches如果是这种情况,我不确定我期望的值会做什么。几乎立即回到0?

  2. 如果/proc/sys/vm/drop_caches设置为3,系统不会进行任何内存缓存,直到重置为0。

哪个案例是真实的?

答案1

它不是粘性的 - 您只需写入文件以使其删除缓存,然后它立即再次开始缓存。

基本上,当您写入该文件时,您并没有真正更改设置,而是向内核发出命令。内核按照该命令执行操作(通过删除缓存),然后像以前一样继续执行。

答案2

它并不像上面所说的那样粘。您也可以使用以下命令进行清理(以及手册中提到的“sync”):

sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"

相关内容