在Linux下频繁存取文件后,物理内存会很快被用光,当程序结束后,内存不会被正常释放,而是一直作为caching. 这样通过free 查看系统内存, 就会发现物理内存剩余很少.
1. free 命令
free 命令详解:
totalusedfreesharedbufferscached
Mem:物理内存已使用物理内存未使用物理内存线程共享内存磁盘写缓存磁盘读缓存-/+buffers/cache:缓存已使用内存缓存未使用内存Swap交互分区总内存交互分区已使用内存交互分区未使用内存
[root@localhost kafka_2.11-0.11.0.0
]
total used
free shared buffers cached
Mem: 7734 1683 6051 3 3 51
-/+ buffers/cache: 1628 6106
Swap: 8023 0 8023
2. 清理缓存
2.1 脚本:freemem
#!/bin/bash
if [ "x$1" = "x" ]; then
limit
=1024
else
limit
=$1
fi
mem_info
(){
used
=`free -m | awk 'NR==2' | awk '{print $3}'`
free
=`free -m | awk 'NR==2' | awk '{print $4}'`
time
=`date +'%Y-%m-%d %H:%M:%S'`
echo "[${time}] used:${used}m, free:${free}m"
}
mem_info
;
if [ $free -le
$limit ] ; then
echo "begin to clean ..."
sync && echo 1
> /proc/sys/vm/drop_caches
sync && echo 2
> /proc/sys/vm/drop_caches
sync && echo 3
> /proc/sys/vm/drop_caches
mem_info
else
echo "nothing to do ..."
fi
2.2 运行脚本
[root@localhost ~
]
[2017-09-13 15:16:37
] used:1875m, free:5859m
nothing to
do ...
[root@localhost ~
]
[2017-09-13 15:16:44
] used:1874m, free:5859m
begin to clean
...
[2017-09-13 15:16:44
] used:1682m, free:6051m