Monkey脚本检测内存泄漏学习小记
2017-08-30 17:38
204 查看
转载自 http://blog.csdn.net/swordgirl2011/article/details/50934413
1. 编写一个带有内存泄漏问题的安卓工程
1.1 布局文件:
[html] view
plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:text="@string/hello_leak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/startBtn"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
1.2 MainActivity代码:
[java] view
plain copy
package com.test.tommyxie.leakcanary;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.startBtn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void start(){
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(20000);
}
}).start();
}
}
2. 编写脚本,实时监控应用内存情况:
[plain] view
plain copy
adb shell dumpsys meminfo com.test.tommyxie.leakcanary |grep Pss
while true
do
adb shell dumpsys meminfo com.test.tommyxie.leakcanary |grep TOTAL
sleep 3
done
3. 运行monkey命令,旋转屏幕,引发内存泄漏:
[plain] view
plain copy
monkey -p com.test.tommyxie.leakcanary --pct-rotation 100 -v -v 10
4. 通过命令获取内存快照,并且讲文件拉到本地:
[plain] view
plain copy
adb shell am dumpheap com.test.tommyxie.leakcanary /sdcard/leak.hprof
[plain] view
plain copy
adb pull /sdcard/leak.hprof
5. 通过hprof-conv转换hprof为MAT工具可分析的格式:
[plain] view
plain copy
hprof-conv leak.hprof new.hprof
6. 使用MAT工具打开转换后的hprof文件,即可进行分析:
安静的偏执的专栏
1. 编写一个带有内存泄漏问题的安卓工程1.1 布局文件:
[html] view
plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:text="@string/hello_leak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/startBtn"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
1.2 MainActivity代码:
[java] view
plain copy
package com.test.tommyxie.leakcanary;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.startBtn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void start(){
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(20000);
}
}).start();
}
}
2. 编写脚本,实时监控应用内存情况:
[plain] view
plain copy
adb shell dumpsys meminfo com.test.tommyxie.leakcanary |grep Pss
while true
do
adb shell dumpsys meminfo com.test.tommyxie.leakcanary |grep TOTAL
sleep 3
done
3. 运行monkey命令,旋转屏幕,引发内存泄漏:
[plain] view
plain copy
monkey -p com.test.tommyxie.leakcanary --pct-rotation 100 -v -v 10
4. 通过命令获取内存快照,并且讲文件拉到本地:
[plain] view
plain copy
adb shell am dumpheap com.test.tommyxie.leakcanary /sdcard/leak.hprof
[plain] view
plain copy
adb pull /sdcard/leak.hprof
5. 通过hprof-conv转换hprof为MAT工具可分析的格式:
[plain] view
plain copy
hprof-conv leak.hprof new.hprof
6. 使用MAT工具打开转换后的hprof文件,即可进行分析:
相关文章推荐
- Monkey脚本检测内存泄漏学习小记
- MFC学习(26)简单内存泄漏检测方法 解决 Detected memory leaks! 问题
- monkey检测内存泄漏
- 根据Facebook内存的管理使用,浅谈在iOS上自动检测内存泄漏问题
- Cocos2d-x学习(二十四):vs2010使用vld检测内存泄露
- 安卓内存泄漏学习(-)--内存泄漏介绍与回收算法简介
- VC使用CRT调试功能来检测内存泄漏
- 【Visual Studio】简单内存泄漏检测方法 解决 Detected memory leaks! 问题(转)
- vs中检测内存泄漏的方法
- MLeaksFinder 自动内存泄漏检测工具
- 使用 Eclipse Memory Analyzer 检测内存泄漏问题
- VS中检测内存泄漏的方法
- 在android上使用valgrind检测内存泄漏
- 如何在linux下检测内存泄漏
- shell脚本检测tomcat进程占用内存大小
- Android之内存泄漏调试学习与总结
- 如何在 Linux 下检测内存泄漏[转载自 Linux爱好者]
- 简单内存泄漏检测方法 解决 Detected memory leaks! 问题
- VC++ 6.0 中如何使用 CRT 调试功能来检测内存泄漏
- 用Xcode6的Leaks检测内存泄漏