メモリリークの調査のときはMemory Analyzer(MAT)がやっぱり強力なので使いたい。でもAndroid StudioのProfilerからダンプしてhprof-convを叩くのは面倒。だからスクリプトにした。
#!/bin/sh
if [ -z "$1" ]; then
echo "USAGE: dumphprof <package_name> [<out_file>]"
exit 1
fi
date=`date '+%Y%m%d%H%M%S'`
temp_file="$1_${date}_orig.hprof"
output_file="$1_${date}.hprof"
if [ -n "$2" ]; then
output_file=$2
fi
echo "dumpheap to /data/local/tmp/${temp_file}"
adb shell am dumpheap $1 /data/local/tmp/${temp_file}
while [ `adb shell run-as $1 lsof -- /data/local/tmp/${temp_file} 2>/dev/null| wc -l` -gt 0 ]; do
sleep 1
done
echo "pull /data/local/tmp/${temp_file}"
adb pull /data/local/tmp/${temp_file}
adb shell rm /data/local/tmp/${temp_file}
echo "hprof-conv to ${output_file}"
hprof-conv ${temp_file} ${output_file}
rm ${temp_file}
echo "hprof file is pulled : ${output_file}"