How to clear memory cache? / 怎么革除内存快取?
cache和buffer都被国内少数人翻译为缓存,这样容易让人分不清cache和buffer有什么区别,为了以示区别,我把cache翻译为快取。
What is buffer? What is cache? / 什么是buffer,什么是 cache?
we refer to memory cache, not CPU cache.
咱们探讨的是内存cache,不是CPU高速寄存器。
Buffer is temporary storage area, data is waiting to be transferred to another place, for example a hard disk.
buffer是一块长期区域,外面的数据被等着传到另一个中央,比方硬盘。
cache is also a memory area, it stores data from files on a disk in advance, so CPU can read that data from cache, faster than from a disk.
cache也是一块内存区域,它提前保留一些在硬盘上的文件数据,这样CPU能够从cache内存读这些数据,比从硬盘上读快。
How to drop memory cache?
You can write to /proc/sys/vm/drop_caches file to instruct kernel to drop caches, as well as reclaimable slab objects like dentries and inodes.
你能够批改/proc/sys/vm/drop_caches以批示内核丢掉快取,和其它占内存的货色,比方目录项和文件inode。
Clear page cache, dentries and inodes in cache memory
echo 3 | sudo tee /proc/sys/vm/drop_caches
Clear dentries and inodes only in cache memory
echo 2 | sudo tee /proc/sys/vm/drop_caches
Clear page cache only in cache memory
echo 31 | sudo tee /proc/sys/vm/drop_caches
You can use 'echo 3 > /proc/sys/vm/drop_caches' directly, you will meet permission error, even if you are root.
你不能间接执行echo 3 > /proc/sys/vm/drop_caches ,你会遇到权限报错,即便你是root用户。
Don't we need to drop memory buffer? 咱们不须要清理内存buffer吗?
Yes, we don't, data in buffer will be transferred out at last.
对,不须要,buffer里的数据终究会被转出(到硬盘)。