轉自:https://www.cnblogs.com/madsnotes/articles/5740495.htmlhtml
頻繁的文件訪問會致使系統的Cache使用量大增。例如:在使用grep從不少文件中搜索特定數據串的時候,發現內存使用迅速提升,主要是cache的使用佔用了至關多的內存。在使用下面命令的時候忽視了文件的數量和文件大小,致使cache突增。node
# grep -e "dst_string" ./*
你們在使用shell編程的時候必定要注意通配符的使用,這裏尤爲提醒你們就是星號(*)的使用,星號當然方便,但要適度使用。 這裏主要仍是記錄如何手動清理linux內存cache,由於上面的操做使用的大量的cache。linux
# 使用free查看當前系統內存使用狀況 [root@bogon ~]# free total used free shared buffers cached Mem: 1938784 89628 1849156 248 60 17644 -/+ buffers/cache: 71924 1866860 Swap: 2097148 0 2097148 # 執行grep操做 [root@bogon ~]# grep -e "dsf" ./* # 再次查看內存使用狀況 [root@bogon ~]# free total used free shared buffers cached Mem: 1938784 100020 1838764 244 3608 26020 -/+ buffers/cache: 70392 1868392 Swap: 2097148 0 2097148
下面介紹如何清理cachedshell
# 執行sync同步數據,防止數據或操做丟失(重要),將未寫的系統緩衝區寫到磁盤中。包含已修改的 i-node、已延遲的塊 I/O 和讀寫映射文件 sync
# 清理cached [root@bogon ~]# echo 3 > /proc/sys/vm/drop_caches # 查看清理cached後的內存使用狀況 [root@bogon ~]# free total used free shared buffers cached Mem: 1938784 87116 1851668 240 188 17596 -/+ buffers/cache: 69332 1869452 Swap: 2097148 0 2097148
drop_cache的詳細文檔以下,以便查閱.編程
Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free. To free pagecache: * echo 1 > /proc/sys/vm/drop_caches To free dentries and inodes: * echo 2 > /proc/sys/vm/drop_caches To free pagecache, dentries and inodes: * echo 3 > /proc/sys/vm/drop_caches As this is a non-destructive operation, and dirty objects are notfreeable, the user should run "sync" first in order to make sure allcached objects are freed. This tunable was added in 2.6.16.(只有內核在2.6.16以上的才支持)