关于linux:怎样清理内存cache

27次阅读

共计 1216 个字符,预计需要花费 4 分钟才能阅读完成。

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

正文完
 0