Linux之清理linux內存cache(整理)


  頻繁的文件訪問會導致系統的Cache使用量大增。例如:在使用grep從很多文件中搜索特定數據串的時候,發現內存使用迅速提高,主要是cache的使用占用了相當多的內存。在使用下面命令的時候忽視了文件的數量和文件大小,導致cache突增。

# grep -e "dst_string" ./*  

  大家在使用shell編程的時候一定要注意通配符的使用,這里尤其提醒大家就是星號(*)的使用,星號固然方便,但要適度使用。  這里主要還是記錄如何手動清理linux內存cache,因為上面的操作使用的大量的cache。

# 使用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

下面介紹如何清理cached

# 執行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以上的才支持

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM