内存调试

转载请注明出处: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 shell指令

1.查看整体内存

连上adb之后 可以通过 adb shell procrank 来查看当前的内存情况!



  • VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
  • RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)
  • PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
  • USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)


2.查看指定进程的内存情况

adb shell dumpsys meminfo (包名或者PID)




3.占用内存最多的进程或线程

adb shell top 

显示当前占用最高内存的10个进程,adb shell top -m 10



查看线程:adb shell top -t -m 10


三.shell 脚本

用于实时监控内存使用情况,并且保存log,我的shell script:

[python]  view plain  copy
 print ?
  1. #!/bin/bash     
  2. echo "Begain test memory">memeory_recode.txt    
  3. i=0    
  4. while true; do    
  5.     
  6. adb shell procrank |grep 'RAM:'| tee -a memeory_record.txt    
  7.      
  8. memoryinfo=$(tail memeory_record.txt -n 1)     
  9. #freememory=$memoryinfo | cut -d ' '-f 4    
  10.     
  11. freememory=`echo "$memoryinfo"|awk -F ' ' '{print $4}'`    
  12.     
  13. free=${freememory%?}    
  14.     
  15. if [ $free -lt 8000 ];then    
  16. echo -e "\033[31mFree Memory is $free KB Less than 8M\033[0m"| tee -a memeory_recode.txt    
  17. adb shell top -m 10 -n 1 | tee -a memeory_recode.txt    
  18. else    
  19. echo "freememory == $free KB"    
  20. fi    
  21.     
  22. i=$(($i+1))    
  23.     
  24.  sleep 1    
  25.     
  26. var=$(date)    
  27. echo "jscese display memory at $var the  $i times"    
  28. echo     
  29. done    


保存RAM信息的情况到 memeory_record.txt,并且解析freememory 的值,如果少于8000K就把占用内存最高的10个进程信息也保存进record。


四.build.prop中的Dalvik设置 

[html]  view plain  copy
 print ?
  1. dalvik.vm.heapstartsize=8m  
  2. dalvik.vm.heapgrowthlimit=96m  
  3. dalvik.vm.heapsize=256m  
  4. dalvik.vm.heaptargetutilization=0.75  
  5. dalvik.vm.heapminfree=512k  
  6. dalvik.vm.heapmaxfree=8m  
  7. dalvik.vm.lockprof.threshold=500  
  8. dalvik.vm.dexopt-flags=m=y  

这几个属性代表了对dalvik的一些属性设置,可以在/dalvik/vm/alloc/HeapSource.cpp下找到原型:

[cpp]  view plain  copy
 print ?
  1. struct HeapSource {  
  2.     /* Target ideal heap utilization ratio; range 1..HEAP_UTILIZATION_MAX 
  3.      */  
  4.     size_t targetUtilization;  
  5.   
  6.     /* The starting heap size. 
  7.      */  
  8.     size_t startSize;  
  9.   
  10.     /* The largest that the heap source as a whole is allowed to grow. 
  11.      */  
  12.     size_t maximumSize;  
  13.   
  14.     /* 
  15.      * The largest size we permit the heap to grow.  This value allows 
  16.      * the user to limit the heap growth below the maximum size.  This 
  17.      * is a work around until we can dynamically set the maximum size. 
  18.      * This value can range between the starting size and the maximum 
  19.      * size but should never be set below the current footprint of the 
  20.      * heap. 
  21.      */  
  22.     size_t growthLimit;  
  23.   
  24.     /* The desired max size of the heap source as a whole. 
  25.      */  
  26.     size_t idealSize;  
  27.   
  28.     /* The maximum number of bytes allowed to be allocated from the 
  29.      * active heap before a GC is forced.  This is used to "shrink" the 
  30.      * heap in lieu of actual compaction. 
  31.      */  
  32.     size_t softLimit;  
  33.   
  34.     /* Minimum number of free bytes. Used with the target utilization when 
  35.      * setting the softLimit. Never allows less bytes than this to be free 
  36.      * when the heap size is below the maximum size or growth limit. 
  37.      */  
  38.     size_t minFree;  
  39.   
  40.     /* Maximum number of free bytes. Used with the target utilization when 
  41.      * setting the softLimit. Never allows more bytes than this to be free 
  42.      * when the heap size is below the maximum size or growth limit. 
  43.      */  
  44.     size_t maxFree;  
  45.   
  46. ...  
  47.   
  48. }  


大体对应的意思如下:

1.heapstartsize——堆初始分配的大小,一个app启动的时候分配的内存大小

2.heapgrowthlimit——分配的一个堆最大的增长值,一个app最多分配的内存大小,超出的话应该会报outofmemory

3.heapsize——整个堆所能达到的最大值,也就是应用程序所能用的内存总和

4.heaptargetutilization——代表堆的利用率,实际使用与最大利用对比

5.heapminfree——堆大小的限制因素,在堆的大小没超过限定值的情况下 最小的空闲值

6.heapmaxfree——和最小相反,堆中最多能空闲的大小

7.lockprof.threshold——调试记录程序内部锁资源争夺的阈值,默认值是500

8.dexopt-flags——程序代码的校验与优化,以下来自百科:

dalvik.vm.dexopt-flags:
本参数控制Dalvik虚拟机的程序代码校验和优化。可填写的值有m、v和o。 m为标准选项,可以是m=y或m=n。若m=y则启用不安全代码的校验和托管代码的优化。兼容性和安全性最高,推荐使用。 v为校验选项,可与o并存。可以是v=a或v=n。若v=a则表示校验所有代码,v=n则关闭代码的校验。 o为优化选项,可与v并存。可以是o=v或o=a。若o=v则表示优化以校验过的代码,o=a则表示优化所有代码。 例如: dalvik.vm.dexopt-flags=m=y dalvik.vm.dexopt-flags=v=n,o=v
注意,这个参数只会影响到安装APK之后或初次使用APK时生成dex文件时有效。若整个系统(包括应用程序)为odex化,则无意义。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值