1. 程式人生 > >CentOS,清理快取(drop_caches)

CentOS,清理快取(drop_caches)

讀寫檔案時,Linux核心為了提高讀寫效率與速度,會將檔案在記憶體中進行快取,這就是Cache Memory(快取記憶體)。
即使程式執行結束後,Cache Memory也不會自動釋放。這就會導致程式頻繁讀寫檔案後,可用實體記憶體會很少。
其實這快取記憶體(Cache Memory)在你需要使用記憶體的時候會自動釋放,所以不必擔心沒有記憶體可用。
如果你希望手動去釋放Cache Memory(快取記憶體)的話也是有辦法的。

清理快取(drop_caches)的官方文件:http://www.kernel.org/doc/Documentation/sysctl/vm.txt

==============================================================
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.
==============================================================
# cat /proc/sys/vm/drop_caches

0//預設是0;1-清空頁快取;2-清空inode和目錄樹快取;3-清空所有快取
[[email protected] ~]# sync //注意:在清空快取之前使用sync命令同步資料到磁碟
[[email protected] ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         15898       3029      12869          0        191       1064
-/+ buffers/cache:       1772      14125
Swap:        31999          0      31999
[
[email protected]
~]# echo 3 > /proc/sys/vm/drop_caches
[[email protected] ~]# free -m //發現快取明顯減少了
             total       used       free     shared    buffers     cached
Mem:         15898       1770      14128          0          1         30
-/+ buffers/cache:       1738      14160
Swap:        31999          0      31999