接前一篇文章:中移(苏州)软件技术有限公司面试问题与解答(0)—— 面试感悟与问题记录
本文参考以下文章:
Linux shell:echo N > /proc/sys/vm/drop_caches清理缓存-CSDN博客
特此致谢!
本文对于中移(苏州)软件技术有限公司面试问题中的“(15)echo 3 > /proc/sys/vm/drop_caches与0、1、2的区别。”进行解答与解析。
/proc/sys/vm/drop_caches
是一个Linux系统中的特殊文件,用于清理内核页缓存、目录项和索引节点缓存。通过写入不同的值到该文件,可以实现不同级别的缓存清理。
Linux内核文档Documentation/admin-guide/sysctl/vm.rst中对于它的详细说明如下:
drop_caches
===========
Writing to this will cause the kernel to drop clean caches, as well as
reclaimable slab objects like dentries and inodes. ?Once dropped, their
memory becomes free.
对其写入将导致内核丢弃干净的缓存,以及可回收的slab对象,如dentries和inode。一旦被丢弃,它们的内存变为free。
To free pagecache::
释放页缓存:
? echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes)::
释放可回收的slab对象(包括dentries和inodes):
? echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache::
释放slab对象和页缓存
? echo 3 > /proc/sys/vm/drop_caches
This is a non-destructive operation and will not free any dirty objects.
To increase the number of objects freed by this operation, the user may run
`sync` prior to writing to /proc/sys/vm/drop_caches. ?This will minimize the
number of dirty objects on the system and create more candidates to be
dropped.
这是一种非破坏性操作,不会释放任何脏对象。
为了增加此操作释放的对象数量,用户可以在写入/proc/sys/vm/drop_caches之前执行“sync”命令。这将最大限度地减少系统上脏对象的数量,并创建更多要删除的候选对象。
This file is not a means to control the growth of the various kernel caches
(inodes, dentries, pagecache, etc...) ?These objects are automatically
reclaimed by the kernel when memory is needed elsewhere on the system.
该文件并非控制各种内核缓存(inode、dentries、pagecache等)增长的手段。当系统其它地方需要内存时,内核会自动回收这些对象。
Use of this file can cause performance problems. ?Since it discards cached
objects, it may cost a significant amount of I/O and CPU to recreate the
dropped objects, especially if they were under heavy use. ?Because of this,
use outside of a testing or debugging environment is not recommended.
使用此文件可能会导致性能问题。由于它会丢弃缓存的对象,因此可能会花费大量的I/O和CPU来重新创建丢弃的对象,尤其是在它们被大量使用的情况下。因此,不建议在测试或调试环境之外使用。
You may see informational messages in your kernel log when this file is
used::
使用此文件时,可以会在内核日志中看到信息性消息
? cat (1234): drop_caches: 3
These are informational only. ?They do not mean that anything is wrong
with your system. ?To disable them, echo 4 (bit 2) into drop_caches.
这些仅供参考。它们并不意味着你的系统有任何问题。要禁用它们,请将4(位2)回声到drop_caches中。
(1)查看缓存命令
在通过echo写入数值之前,先查看/proc/sys/vm/drop_caches的默认值(以便可以修改回来)。实际命令及结果如下(在笔者电脑上):
$ cat /proc/sys/vm/drop_caches
cat: /proc/sys/vm/drop_caches: Permission denied
$ sudo cat /proc/sys/vm/drop_caches
[sudo] password for ph:
cat: /proc/sys/vm/drop_caches: Permission denied
$ ls /proc/sys/vm/drop_caches -l
--w------- 1 root root 0 1月 20 08:54 /proc/sys/vm/drop_caches
# chmod 0600 /proc/sys/vm/drop_caches
chmod: changing permissions of '/proc/sys/vm/drop_caches': Operation not permitted
看来在Ubuntu 22.04中,对于/proc/sys/vm/drop_caches不允许修改权限。
(2)查看内存使用情况
通过free命令查看内存使用情况。实际命令及结果如下:
$ free
total used free shared buff/cache available
Mem: 6022708 1112536 410588 17064 4499584 4596248
Swap: 2097148 555116 1542032
$ free -m
total used free shared buff/cache available
Mem: 5881 1086 400 16 4394 4488
Swap: 2047 542 1505
$ free -h
total used free shared buff/cache available
Mem: 5.7Gi 1.1Gi 410Mi 16Mi 4.3Gi 4.4Gi
Swap: 2.0Gi 542Mi 1.5Gi
关于free命令的详解,可以参考:Linux free命令使用
1)Mem行
total used free shared buff/cache available
Mem: 5881 1086 400 16 4394 4488
?
(3)清除缓存命令
1)不释放缓存
数字1用来清空最近放问过的文件页面缓存。实际命令及结果如下:
# echo 0 > /proc/sys/vm/drop_caches
bash: echo: write error: Invalid argument
2)释放页缓存(pagechche)
数字2用来清空文件节点缓存和目录项缓存。实际命令及结果如下:
# echo 1 > /proc/sys/vm/drop_caches
#
# free -m
total used free shared buff/cache available
Mem: 5881 1059 4091 16 729 4515
Swap: 2047 542 1505
3)释放dentries和inodes缓存
实际命令及结果如下:
# echo 2 > /proc/sys/vm/drop_caches
#
# free -m
total used free shared buff/cache available
Mem: 5881 1039 4539 16 302 4578
Swap: 2047 542 1505
4)清空1)和2)中的所有缓存
数字3用来清空1和2所有内容的缓存。实际命令及结果如下:
# echo 3 > /proc/sys/vm/drop_caches
#
# free -m
total used free shared buff/cache available
Mem: 5881 1047 4532 16 302 4569
Swap: 2047 542 1505
更多内容请看下回。