实时统计程序所占内存情况,查内存泄漏用
#!/bin/bash
# 有错就退出
set -e
# 程序名
processName="Debug/tccmt/tccmt"
# 进程ID
processId=""
# 输出文件
outFile="/tmp/mem"
if [ $# -eq 1 ]; then
processName=$1
elif [ $# -eq 2 ];then
processName=$1
processId=$2
elif [ $# -eq 3 ];then
processName=$1
processId=$2
outFile=$3
fi
echo "processName: ${processName}, processId:${processId}, outFile=${outFile}"
# 统计间隔 (单位:秒)
period=30
# 文件存在则删除
if [ -e ${outFile} ]; then
rm $outFile
fi
# 查看pid
if [ -z ${processId} ]; then
processId=`ps -ef | grep ${processName} | head -1 | tr -s ' ' | cut -f2 -d' '`
echo "grpe pid is $processId"
fi
while true
do
time=`date "+%Y-%m-%d %H:%M:%S"`
#echo $time
# 查看进程所占用的内存
useMem=`sudo cat /proc/${processId}/status | grep RssAnon`
#echo $useMem
msg="${time} ${useMem}"
echo $msg
# 同时记录到文件
echo $msg >> $outFile
# 10s统计一次
sleep $period
done
网友评论