Monkey脚本检测内存泄漏学习小记

转载自 http://blog.csdn.net/swordgirl2011/article/details/50934413

安静的偏执的专栏 


1. 编写一个带有内存泄漏问题的安卓工程

1.1 布局文件:

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.                 xmlns:tools="http://schemas.android.com/tools"  
  3.                 android:layout_width="match_parent"  
  4.                 android:layout_height="match_parent"  
  5.                 android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.                 android:paddingRight="@dimen/activity_horizontal_margin"  
  7.                 android:paddingTop="@dimen/activity_vertical_margin"  
  8.                 android:paddingBottom="@dimen/activity_vertical_margin"  
  9.                 tools:context=".MainActivity">  
  10.   
  11.     <TextView  
  12.         android:text="@string/hello_leak"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"/>  
  15.   
  16.     <Button  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="Start"  
  20.         android:id="@+id/startBtn"  
  21.         android:layout_centerVertical="true"  
  22.         android:layout_centerHorizontal="true"/>  
  23.   
  24. </RelativeLayout>  

1.2 MainActivity代码:

[java]  view plain  copy
  1. package com.test.tommyxie.leakcanary;  
  2.   
  3. import android.os.Bundle;  
  4. import android.os.SystemClock;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.view.Menu;  
  7. import android.view.MenuItem;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         Button button = (Button)findViewById(R.id.startBtn);  
  18.         button.setOnClickListener(new View.OnClickListener() {  
  19.             @Override  
  20.             public void onClick(View v) {  
  21.                 start();  
  22.             }  
  23.         });  
  24.     }  
  25.   
  26.     @Override  
  27.     public boolean onCreateOptionsMenu(Menu menu) {  
  28.         // Inflate the menu; this adds items to the action bar if it is present.  
  29.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  30.         return true;  
  31.     }  
  32.   
  33.     @Override  
  34.     public boolean onOptionsItemSelected(MenuItem item) {  
  35.         // Handle action bar item clicks here. The action bar will  
  36.         // automatically handle clicks on the Home/Up button, so long  
  37.         // as you specify a parent activity in AndroidManifest.xml.  
  38.         int id = item.getItemId();  
  39.   
  40.         //noinspection SimplifiableIfStatement  
  41.         if (id == R.id.action_settings) {  
  42.             return true;  
  43.         }  
  44.   
  45.         return super.onOptionsItemSelected(item);  
  46.     }  
  47.   
  48.     public void start(){  
  49.         new Thread(new Runnable() {  
  50.             @Override  
  51.             public void run() {  
  52.                 SystemClock.sleep(20000);  
  53.             }  
  54.         }).start();  
  55.     }  
  56. }  


2. 编写脚本,实时监控应用内存情况:

[plain]  view plain  copy
  1. adb shell dumpsys meminfo com.test.tommyxie.leakcanary |grep Pss  
  2. while true  
  3. do  
  4. adb shell dumpsys meminfo com.test.tommyxie.leakcanary |grep TOTAL  
  5. sleep 3  
  6. done  

3. 运行monkey命令,旋转屏幕,引发内存泄漏:

[plain]  view plain  copy
  1. monkey -p com.test.tommyxie.leakcanary --pct-rotation 100 -v -v 10  


4. 通过命令获取内存快照,并且讲文件拉到本地:

[plain]  view plain  copy
  1. adb shell am dumpheap com.test.tommyxie.leakcanary /sdcard/leak.hprof   
[plain]  view plain  copy
  1. adb pull /sdcard/leak.hprof  

5. 通过 hprof-conv 转换hprof为MAT工具可分析的格式:

[plain]  view plain  copy
  1. hprof-conv leak.hprof new.hprof  

6. 使用MAT工具打开转换后的hprof文件,即可进行分析:


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值