android系统内存泄露统计

来源:互联网 发布:提升淘宝店铺权重 编辑:程序博客网 时间:2024/05/29 17:28

#!/data/busybox sh

/data/busybox mkdir -p /data/log

chmod 777 /data/log

while [ 1 == 1 ];

do

current_time=`date | /data/busybox tr -d [:[:blank:]]`;

echo "the current time is: $current_time" >>/data/log/mem_info$current_time

ps > /data/log/ps_state

processes=`cat /data/log/ps_state | /data/busybox awk '{print $2}'`

for i in $processes

do

if [ ! -e /proc/$i ];then

continue;

fi

if [ ! -e /proc/$i/status ];then

continue;

fi

process_name=`cat /proc/$i/cmdline`

echo -e "at time: "`date`" ,the process name:$process_name,pid $i, status is:\n" >> /data/log/mem_info$current_time

cat /proc/$i/status >> /data/log/mem_info$current_time

echo -e "\n" >> /data/log/mem_info$current_time

done

rm /data/log/ps_state

sleep 3600


done


内存信息都保存在了/data/log/mem_info*的文件里面,统计一次有一个这样的文件;

分析这些文件就可以看出有没有内存泄露了,可以用beyondcompare,或者再写一个脚本就可以看出哪些进程有泄漏了;

简单的分析脚本如下:
cat get_clean_info.sh 
if [ $# -lt 2 ];then
        echo "Usage $0 infile outfile"
        exit
fi
rm tmp_file tmp_name_rss
in_file=$1;
out_file=$2
cat $in_file| grep  -E 'the current|Name|Mem|Total|Swap|VmRSS'  > tmp_file
let i=0;
while read line 
do
result=`echo $line | grep -E -c "the current|Mem:|Swap:|Total:" `
if [ $result -lt 1 ];then
        is_name=`echo $line | grep -c "Name:"`
        if [ $is_name -eq 1 ];then
                name=`echo $line | awk '{print $2}'`;
                continue;
        else
                is_rss=`echo $line |grep -c "VmRSS:"`
                if [ $is_rss -eq 1 ];then
                out_line=$name"  "$line
                echo "the out_line is $out_line"
                echo "$out_line" >>tmp_name_rss
                fi
        fi
else
echo "$line" >> $out_file
fi
done < tmp_file
sleep 2
cat tmp_name_rss | sort >>$out_file

使用方法:
./get_clean_info.sh input_file output_file
对于两个样本文件执行两次这个动作,就可以看到比较简单的内存占用情况;

原文:http://blog.chinaunix.net/uid-741742-id-359324.html
原创粉丝点击