欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux释放内存分析

程序员文章站 2022-06-04 11:13:24
...

Linux释放内存分析

理论分析:

/proc/sys/vm/drop_caches文件的值用于控制内存,其值可以是0-3之间的数字(Linux系统默认该值为0),代表不同的含义:
0:不释放(系统默认值)
1:释放页缓存
2:释放dentries和inodes
3:释放所有缓存

释放内存执行:echo 1 > /proc/sys/vm/drop_caches
释放完内存后改回去让系统重新自动分配内存:echo 0 >/proc/sys/vm/drop_caches

释放所有缓存,执行:echo 3 > /proc/sys/vm/drop_caches

案例:

[[email protected] ~]# free
             total       used       free     shared    buffers     cached
Mem:       1020276     568484     451792          0       2580      35964
-/+ buffers/cache:     529940     490336
Swap:            0          0          0
[[email protected] ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           996        725        271          0         69        126
-/+ buffers/cache:        528        467
Swap:            0          0          0
[[email protected] ~]# echo 3 > /proc/sys/vm/drop_caches
[[email protected] ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           996        551        444          0          9         29
-/+ buffers/cache:        513        482
Swap:            0          0          0
[[email protected] ~]# echo 0 > /proc/sys/vm/drop_caches
[[email protected] ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           996        569        427          0         26         29
-/+ buffers/cache:        513        482
Swap:            0          0          0
[[email protected] ~]#