web-dev-qa-db-ja.com

/ proc / sys / vm / drop_cachesを設定してキャッシュをクリアする

いくつかのコールドキャッシュタイミングを実行する一環として、OSキャッシュを解放しようとしています。 カーネルのドキュメント(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

pagecache、dentries、inodeを解放します。 OK。

それで、システムが通常どおりにキャッシュを開始するようにしたい場合、最初にそれを0にリセットする必要がありますか?私のシステムの値は現在0に設定されています。これがデフォルトであると想定しています。それとも自動的にリセットされますか?ここでは少なくとも2つの可能性が考えられますが、どちらが本当かはわかりません。

  1. echo 3 > /proc/sys/vm/drop_cachesは、ページキャッシュ、エントリ、およびiノードを解放します。その後、システムはすぐに再びキャッシュを開始します。これが事実である場合、/proc/sys/vm/drop_cachesの値に何を期待するかわかりません。ほとんどすぐに0に戻りますか?

  2. /proc/sys/vm/drop_cachesが3に設定されている場合、システムは0にリセットされるまでメモリキャッシュを行いません。

どちらが本当ですか?

89
Faheem Mitha

スティッキーではありません。ファイルに書き込んでキャッシュを削除すると、すぐにキャッシュが再開されます。

基本的に、そのファイルに書き込むとき、実際には設定を変更していませんが、カーネルにコマンドを発行しています。カーネルは(キャッシュを削除することにより)そのコマンドを実行し、以前と同様に続行します。

114
TomH