13 May, 2013

Investigation of android apps memory consumption (looking for a leaking apps)

This technique may be useful for detecting application with memory leakage. Example below focused on com.android.systemui and system process, but you can easy modify and improve it for yourself.



The main idea is to collect memory usage data during few day and then analyse dynamic of memory usage by app. For collecting this data I use SL4A and simple script.

cd /sdcard/debug_info/meminfo;
while [ 1 ]
 do
 FNAME=`date +%F_%H-%M-%S`
 touch $FNAME
 su -c "dumpsys meminfo > $FNAME";
 sleep 600
done

After several days script generates hundreds of files for analysis. Now it should perform mass renaming to "*.log" and then convert all files into one cvs file by using next python script (process names has hardcoded, so you need modify it for you porpose).

import sys
import re
import os
import fnmatch
import glob

fh = open('meminfo.cvs','w')
for file in glob.glob('*.log'):
    fo = open(file,'r')   
    cvs_row = ''
    for line in fo.readlines():
        if re.search(r'Uptime:', line):
            words = line.split()
            cvs_row += words[1] + ';'
            break
    fo.seek(0)
    for line in fo.readlines():           
        if re.search(r': system', line):
            words = line.split()
            cvs_row += words[0] + ';'
            break
    fo.seek(0)
    for line in fo.readlines():           
        if re.search(r': com.android.systemui', line):
            words = line.split()
            cvs_row += words[0] + ';'
            break
    fo.seek(0)
    for line in fo.readlines():
        if re.search(r'Total PSS: ', line):
            words = line.split()
            cvs_row += words[2] + ';\n'
            break
    fo.close()
    print cvs_row
    fh.writelines(cvs_row)
fh.close()

And finally it is possible generate graph by using next python script

import csv
import matplotlib.pyplot as plt

from numpy import  *

import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt


list_uptime = []
list_sysui = []
list_sys = []

with open('meminfo.cvs', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=';', quotechar='|')
    for row in reader:
        list_uptime.append(row[0])
        list_sys.append(row[1])
        list_sysui.append(row[2])

uptime_arr = array(list_uptime, float)
sys_ar = array(list_sys, float)
sysui_ar = array(list_sysui, float)

sym_ar = sys_ar+sysui_ar
uptime_in_sec_arr = uptime_arr/1000
uptime_in_hour_arr = uptime_in_sec_arr/3600
uptime_in_day_arr = uptime_in_hour_arr / 24

# plt.scatter(uptime_in_hour_arr, sys_ar, s=40, marker='o', c='r', label='system')
plt.scatter(uptime_in_hour_arr, sysui_ar, s=40, marker='o', c='c', label='systemui')

plt.xlabel('Uptime, hours')
plt.ylabel('Memory usage, KB')
plt.title('Memory usage vs Uptime')
plt.legend(loc='upper left')

plt.show()

After all you get graph like below and can explore the bugged application more closely.


No comments:

Post a Comment