CentOS 釋放內存

內存佔用原因
當讀寫文件的時候,Linux內核爲了提高讀寫性能與速度,會將文件在內存中進行緩存,這部分內存就是Cache Memory.
即使你的程序運行結束後,Cache Memory也不會自動釋放.這就會導致你在Linux系統中程序頻繁讀寫文件後,你會發現可用物理內存會很少.

 

處理方法一
Cache Memory在你需要使用內存的時候會自動釋放,所以你不必擔心沒有內存可用.

處理方法二
如果你希望手動去釋放Cache Memory也是有辦法的:
使用 sync;sync;echo 1 > /proc/sys/vm/drop_caches 命令.

效果很顯著.


命令解釋
sync命令的作用是將有關文件系統的存儲器常駐信息送入物理介質內.
爲確保可靠起見,應執行兩遍sync命令,這是因爲sync命令完成時,並不保證信息實際寫到了磁盤上.

 

清空 pagecache
echo 1 > /proc/sys/vm/drop_caches
sysctl -w vm.drop_caches=1命令與上述命令效果相同

 

清空所有緩存(pagecache、dentries 和 inodes)
echo 3 > /proc/sys/vm/drop_caches
sysctl -w vm.drop_caches=3命令與上述命令效果相同

 

關於drop_caches的官方說法
To use /proc/sys/vm/drop_caches, just echo a number to it.
 
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

This is a non-destructive operation and will only free things that are completely unused. Dirty objects will continue to be in use until written out to disk and are not freeable. If you  run "sync" first to flush them out to disk, these drop operations will tend to free more memory.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章