1. 程式人生 > >磁碟快取增大, 佔據記憶體清除的方法

磁碟快取增大, 佔據記憶體清除的方法

大家有沒有遇到這種情況:在給tf卡或者usb儲存裝置持續寫入檔案的時候,記憶體佔用會不斷增加,一直到剩餘很少記憶體的時候才停止佔用更多記憶體。而這時候  如果其他模組急需更多記憶體,系統執行效率就會大大降低。最近google+baidu  翻閱了一些資料做如下總結:

頻繁的檔案訪問會導致系統的Cache使用量大增,佔用記憶體。

執行sync命令將dirty的內容寫回硬碟
$sync

通過修改proc系統的drop_caches清理free的cache
$echo 3 > /proc/sys/vm/drop_caches

drop_caches的詳細文件如下:
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

linux C 程式設計序可以用sync()函式替代命令。