怎样清理内存cache?

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里的数据终究会被转出(到硬盘)。

你可能感兴趣的:(linux)