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

来源:互联网 发布:免费下载音乐的软件 编辑:程序博客网 时间:2024/06/10 13:38

转载自 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文件,即可进行分析:


原创粉丝点击