Android App 内存泄漏检查工具MAT

Eclipse 有个插件工具MAT(Memory Analyzer Tool)可以帮助定位内存泄漏的对象。

  1. 安装MAT Update site: http://archive.eclipse.org/mat/1.1/update-site/
  2. 用DDMS工具Dump出问题App的.hprof文件 比如com.world.test2.hprof Dump之前最好先运行一下GC "Cause GC" , 确保dump出来的是还不能回收的对象等。
  3. 用SDK tools下工具hprof-conv.exe 做转换 hprof-conv com.world.test2.hprof appleak.hprof
  4. 用Eclipse “Open Head Dump”打开新转换的.hprof 文件--appleak.hprof 查看图形化界面,一个一个检查怀疑的点。

总结: MAT tool不会直接告诉你哪里内存泄漏,但是会列出怀疑的对象,需要你仔细检查这些对象为什么没有被释放掉。

下面是测试code, 在Android 4.2.2上测试过。 1. 此种情况可以引起Activity无法回收的情况,因为直接用类似private static Activity a0引用创建的Activity,导致Activity无法回收。 2. 此种情况没有引起Activity 无法回收的情况。 按理说这种情况应该也会导致静态Drawable 锁定Activity, 引用关系mBackground1-->Button-->Activity. 待分析

 
  1. public class MainActivity extends Activity implements Button.OnClickListener{
  2. final private static String TAG = "MainActivity";
  3. private static Drawable mBackground1;
  4. private static Drawable mBackground2;
  5. private static Drawable mBackground3;
  6. private static Drawable mBackground4;
  7. private static Activity a0 ;
  8. private static Activity a1 ;
  9. private static Activity a2 ;
  10. private static Activity a3 ;
  11. private static Activity a4 ;
  12. /*
  13. * Shutdown intent
  14. */
  15. private final String INTENT_ACTION_REQUEST_SHUTDOWN =
  16. "android.intent.action.ACTION_REQUEST_SHUTDOWN";
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. Log.v(TAG, "onCreate Activity="+this);
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. //1
  23. if(false){
  24. if(a0 == null){
  25. a0 = this;
  26. Log.v(TAG, "onCreate Activity a0="+a0);
  27. }
  28. else if(a1 == null){
  29. a1 = this;
  30. Log.v(TAG, "onCreate Activity a1="+a1);
  31. }
  32. else if(a2 == null){
  33. a2 = this;
  34. Log.v(TAG, "onCreate Activity a2="+a2);
  35. }
  36. else if(a3 == null){
  37. a3 = this;
  38. Log.v(TAG, "onCreate Activity a3="+a3);
  39. }
  40. else if(a4 == null){
  41. a4 = this;
  42. Log.v(TAG, "onCreate Activity a4="+a4);
  43. }
  44. }
  45. //set up button listener
  46. Button myButton = (Button)findViewById(R.id.button_poweroff);
  47. myButton.setOnClickListener(this);
  48. myButton = (Button)findViewById(R.id.button_reboot);
  49. myButton.setOnClickListener(this);
  50. //2
  51. if (mBackground1 == null) {
  52. Log.v(TAG, "onCreate mBackground1");
  53. mBackground1 = getResources().getDrawable(R.drawable.adbroot_004);
  54. myButton.setBackgroundDrawable(mBackground1);
  55. }
  56. else if(mBackground2 == null){
  57. Log.v(TAG, "onCreate mBackground2");
  58. mBackground2 = getResources().getDrawable(R.drawable.test002);
  59. myButton.setBackgroundDrawable(mBackground2);
  60. }
  61. else if(mBackground3 == null){
  62. Log.v(TAG, "onCreate mBackground3");
  63. mBackground3 = getResources().getDrawable(R.drawable.test003);
  64. myButton.setBackgroundDrawable(mBackground3);
  65. }
  66. else if(mBackground4 == null){
  67. Log.v(TAG, "onCreate mBackground4");
  68. mBackground4 = getResources().getDrawable(R.drawable.adbroot_003);
  69. myButton.setBackgroundDrawable(mBackground4);
  70. }
  71. }

【编辑推荐】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值