Linux手动清理内存
在日常运维过程中,Linux系统会遇到将程序缓存到内存中,并且不会释放已经占用了系统本身的内存,将系统的内存利用率达到百分之九十几,出现这种情况,我们就需要手动清理一下操作系统的内存。
操作步骤:
- 查询当前系统的剩余内存
- 查询/proc/sys/vm/drop_caches的当前值
- 同步内存中缓存的数据到磁盘
- 修改/proc/sys/vm/drop_caches的值
查询系统的当前剩余内存:
[root@localhost Desktop]# free -m
total used free shared buffers cached
Mem: 3953 1479 2474 0 56 1008
-/+ buffers/cache: 414 3539
Swap: 2047 0 2047
[root@localhost Desktop]#
查询/proc/sys/vm/drop_caches的值,默认为0
[root@localhost Desktop]# cat /proc/sys/vm/drop_caches
0
手动执行sync命令(描述:sync 命令运行 sync 子例程。如果必须停止系统,则运行sync 命令以确保文件系统的完整性。sync 命令将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件)
[root@localhost Desktop]# sync
修改/proc/sys/vm/drop_caches的值为1、2、3(三个值中其中一个)
[root@localhost Desktop]# echo 1 >/proc/sys/vm/drop_caches
[root@localhost Desktop]# free -m
total used free shared buffers cached
Mem: 3953 462 3491 0 0 72
-/+ buffers/cache: 390 3563
Swap: 2047 0 2047
drop_caches的相关介绍:
/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,
dentries and inodes from memory, causing that memory to become
free.
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches; to
free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 >
/proc/sys/vm/drop_caches.Because this is a non-destructive operation and dirty objects
are not freeable, the user should run sync first.
至此,我们就完成了Linux系统内存的清理工作。
『—ENDOF—』