因调试某个重大问题,怀疑到了内存,专门写了个测试脚本,记录一下。
撰写不易,转载请注明出处:http://blog.csdn.net/jscese/article/details/37928823
首先需要开启android系统的adb功能,start adbd
PC端 adb connect IP ADDRESS
如果 出现adb 异常可以尝试 adb kill-server ; adb start-server
连上adb之后 可以通过 adb shell procrank 来查看当前的内存情况!
adb shell dumpsys meminfo (包名或者PID)
adb shell top
显示当前占用最高内存的10个进程,adb shell top -m 10:
查看线程:adb shell top -t -m 10
用于实时监控内存使用情况,并且保存log,我的shell script:
#!/bin/bash echo "Begain test memory">memeory_recode.txt i=0 while true; do adb shell procrank |grep 'RAM:'| tee -a memeory_record.txt memoryinfo=$(tail memeory_record.txt -n 1) #freememory=$memoryinfo | cut -d ' '-f 4 freememory=`echo "$memoryinfo"|awk -F ' ' '{print $4}'` free=${freememory%?} if [ $free -lt 8000 ];then echo -e "\033[31mFree Memory is $free KB Less than 8M\033[0m"| tee -a memeory_recode.txt adb shell top -m 10 -n 1 | tee -a memeory_recode.txt else echo "freememory == $free KB" fi i=$(($i+1)) sleep 1 var=$(date) echo "jscese display memory at $var the $i times" echo done
保存RAM信息的情况到 memeory_record.txt,并且解析freememory 的值,如果少于8000K就把占用内存最高的10个进程信息也保存进record。
dalvik.vm.heapstartsize=8m dalvik.vm.heapgrowthlimit=96m dalvik.vm.heapsize=256m dalvik.vm.heaptargetutilization=0.75 dalvik.vm.heapminfree=512k dalvik.vm.heapmaxfree=8m dalvik.vm.lockprof.threshold=500 dalvik.vm.dexopt-flags=m=y
struct HeapSource { /* Target ideal heap utilization ratio; range 1..HEAP_UTILIZATION_MAX */ size_t targetUtilization; /* The starting heap size. */ size_t startSize; /* The largest that the heap source as a whole is allowed to grow. */ size_t maximumSize; /* * The largest size we permit the heap to grow. This value allows * the user to limit the heap growth below the maximum size. This * is a work around until we can dynamically set the maximum size. * This value can range between the starting size and the maximum * size but should never be set below the current footprint of the * heap. */ size_t growthLimit; /* The desired max size of the heap source as a whole. */ size_t idealSize; /* The maximum number of bytes allowed to be allocated from the * active heap before a GC is forced. This is used to "shrink" the * heap in lieu of actual compaction. */ size_t softLimit; /* Minimum number of free bytes. Used with the target utilization when * setting the softLimit. Never allows less bytes than this to be free * when the heap size is below the maximum size or growth limit. */ size_t minFree; /* Maximum number of free bytes. Used with the target utilization when * setting the softLimit. Never allows more bytes than this to be free * when the heap size is below the maximum size or growth limit. */ size_t maxFree; ... }
大体对应的意思如下:
1.heapstartsize——堆初始分配的大小,一个app启动的时候分配的内存大小
2.heapgrowthlimit——分配的一个堆最大的增长值,一个app最多分配的内存大小,超出的话应该会报outofmemory
3.heapsize——整个堆所能达到的最大值,也就是应用程序所能用的内存总和
4.heaptargetutilization——代表堆的利用率,实际使用与最大利用对比
5.heapminfree——堆大小的限制因素,在堆的大小没超过限定值的情况下 最小的空闲值
6.heapmaxfree——和最小相反,堆中最多能空闲的大小
7.lockprof.threshold——调试记录程序内部锁资源争夺的阈值,默认值是500
8.dexopt-flags——程序代码的校验与优化,以下来自百科:
原文:http://blog.csdn.net/jscese/article/details/37928823