我们可以使用代码生成hprof,然后使用批处理hprof文件,然后用MAT进行对多个hprof文件比较分析。
<wbr style="line-height:25px"><span style="line-height:25px"><span style="font-size:16px; line-height:28px">一、批处理配置文件</span></span>
<div style="line-height:25px">批处理配置文件config.bat如下:</div>
<div style="line-height:25px">
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">rem the following var is for getProcessState.bat</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set rawDatadir=rawData</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set processName=com.sec.android.app.dialertab;android.process.acore;com.sec.android.provider.logsprovider</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">rem set processShortName=a;b;c</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set processShortName=</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set outRoot=out</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set statFilePrefix=stat</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">rem the following var is for getHprof.bat</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set tools=D:\SDK\froyo\android-sdk-Dempsey-I997\tools</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set hpInputFileDir=/sdcard</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set hpInputFile=input.hprof</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set hpRoot=hpTemp</span></div>
<div style="line-height:25px">
<span style="line-height:25px">注1</span>:rawDatadir为“ps -x”提取出来的文件的目录</div>
<div style="line-height:25px">
<span style="line-height:25px">注2</span>:processName需要统计RSS的进程的名字,可以同时统计多个,进程名之间用“;”进行分割。</div>
<div style="line-height:25px">
<span style="line-height:25px">注3</span>:processShortName需要统计RSS的进程的名字的缩写形式,如果不坐设置或设置为空,这程序会根据processName自动生成。</div>
<div style="line-height:25px">
<span style="line-height:25px">注4</span>:outRoot为对进程的RSS进行统计最后的生成文件的存放目录。</div>
<div style="line-height:25px">
<span style="line-height:25px">注5</span>:outRoot为对进程的RSS进行统计最后的生成文件的前缀。</div>
<div style="line-height:25px">
<span style="line-height:25px">注6</span>:tools为hprof-conv.exe所在的目录。</div>
<div style="line-height:25px">
<span style="line-height:25px">注7</span>:hpInputFileDir为手机中我们生成的hprof文件所在的目录。</div>
<div style="line-height:25px">
<span style="line-height:25px">注8</span>:<span style="line-height:22px">hpInputFileDir为</span><span style="line-height:22px">手机中我们生成的</span><span style="line-height:22px">hprof文件的名字。</span>
</div>
<div style="line-height:25px"><span style="line-height:25px"><span style="font-size:16px; line-height:28px">二,需要使用的bat库</span></span></div>
<div style="line-height:25px"><span style="font-size:16px; line-height:28px"><span style="line-height:25px"><span style="line-height:22px; font-size:14px">子目录</span><span style="line-height:22px; font-size:14px"><span style="color:#003366; line-height:25px">lib</span></span><span style="line-height:22px; font-size:14px">用于存放</span><strong><span style="color:#003366; line-height:25px">bat库</span></strong></span></span></div>
<div style="line-height:25px">需要的bat库:<span style="color:#0000ff; line-height:25px">genSerial.bat</span><span style="color:#003366; line-height:25px">,</span><span style="color:#0000ff; line-height:25px">getSubStr.bat</span><span style="color:#003366; line-height:25px">。</span>
</div>
<div style="line-height:25px">
<span style="color:#003366; line-height:25px">关于</span><span style="color:#0000ff; line-height:25px">genSerial.bat</span><span style="color:#003366; line-height:25px">请参看《</span><strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201132224645268/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">genSerial</a></strong><span style="line-height:25px; color:rgb(0,51,102)">》</span>
</div>
<div style="line-height:25px">
<span style="line-height:25px; color:rgb(0,51,102)">关于</span><span style="color:#0000ff; line-height:25px">getSubStr.bat</span><span style="line-height:25px; color:rgb(0,51,102)">请参看《</span><strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201132232153255/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">getSubStr</a></strong><span style="line-height:25px; color:rgb(0,51,102)">》</span>
</div>
<div style="line-height:25px"><span style="line-height:25px">三,在代码中生成Hprof文件:</span></div>
<div style="line-height:25px"><span style="color:#003366; line-height:25px">在android代码,可以使用如下代码把hprof文件生成到sd卡上。</span></div>
<div style="line-height:25px"><span style="color:#0000ff; line-height:25px">Debug.dumpHprofData("/sdcard/input.hprof");</span></div>
<div style="line-height:25px"><span style="color:#000080; line-height:25px">可以不用sd卡,而将hprof文件直接生成在手机上,但是只能在"/data/data/"+packageName的目录下。</span></div>
<div style="line-height:25px">
<span style="line-height:25px">实例1</span><span style="color:#003366; line-height:25px">:</span>
</div>
<div style="line-height:25px">
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"> void generateHprof()</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"> {</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>String packageName=getApplicationInfo().packageName;</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>String hpFilePath="/data/data/"+packageName+"/input.hprof";</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>try {</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>//Debug.dumpHprofData("/sdcard/input.hprof");</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>Debug.dumpHprofData(hpFilePath);</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>} catch (IOException e) {</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>// TODO Auto-generated catch block</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>e.printStackTrace();</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>}</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"> }</span></div>
</div>
<div style="line-height:25px"><span style="line-height:25px"><span style="font-size:16px; line-height:28px">四,在电脑上通过批处理取转换Hprof 文件</span></span></div>
<div style="line-height:25px">
<span style="color:#003366; line-height:25px">如果在程序中用</span><span style="color:#0000ff; line-height:25px">Debug.dumpHprofData("/sdcard/input.hprof")</span><span style="color:#003366; line-height:25px">的方式生成了</span><span style="line-height:25px; color:rgb(0,51,102)">hprof文件</span><span style="color:#003366; line-height:25px">,</span>
</div>
<div style="line-height:25px">
<span style="color:#003366; line-height:25px">那么就可以执行文件getHprof .bat来取得</span><span style="line-height:25px; color:rgb(0,51,102)">Hprof并转化为MemoryAnalyzer的格式。</span>
</div>
<div style="line-height:25px">getHprof.ba<span style="color:#003366; line-height:25px">t文件如下:</span>
</div>
<div style="line-height:25px">
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">@echo off</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">call config.bat</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">if exist %hpInputFile% (</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>del %hpInputFile% /q</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">)</span></div>
<div style="line-height:25px"><span style="color:#0000ff; line-height:25px">adb pull %hpInputFileDir%/%hpInputFile% .</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">if not exist %hpInputFile% (</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>echo fail to pull %hpInputFile%</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>exit 1</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">)</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">if not exist %hpRoot% (</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px"><span style="line-height:25px; white-space:pre"></span>md %hpRoot%</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">)</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">Setlocal enabledelayedexpansion</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set path=%path%;%cd%\lib</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">call genSerial</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set serial=!genSerial~result!</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">set hpOutFile=%serial%.hprof</span></div>
<div style="line-height:25px"><span style="color:#0000ff; line-height:25px">%tools%\hprof-conv.exe %hpInputFile% %hpRoot%\%hpOutFile%</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">echo success!</span></div>
<div style="line-height:25px"><span style="color:#3366FF; line-height:25px">endlocal</span></div>
</div>
<div style="line-height:25px">
<span style="line-height:25px">注1</span>:<span style="line-height:25px; color:rgb(0,51,102)">需要在代码中先生成</span><span style="color:#0000ff; line-height:25px">hprof文件</span><span style="line-height:25px; color:rgb(0,51,102)">,而且config.bat中配置的路径要和</span><span style="line-height:22px; color:rgb(0,51,102)">在代码中先生成</span><span style="line-height:22px; color:rgb(0,51,102)">hprof文件的路径一致。</span>
</div>
<div style="line-height:25px">
<div style="line-height:22px">
<span style="line-height:22px">注2</span><span style="line-height:22px"><span style="color:#003366; line-height:25px">:</span><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px"><span style="color:#003366; line-height:25px">如果需要连续多次以固定的时间</span><span style="color:#0000ff; line-height:25px">提取</span></span></span><span style="color:#0000ff; line-height:25px">hprof文件</span><span style="line-height:25px; color:rgb(0,51,102)">,请参照《</span><strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792011324114317115/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Android内存泄露利器(全自动篇)</a></strong><span style="line-height:25px; color:rgb(0,51,102)">》</span>
</div>
<div style="line-height:25px">
<span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px"></span>
<div style="line-height:25px">
<span style="line-height:25px">注意3</span><span style="color:#003366; line-height:25px">:<span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53">需要把bat库<span style="line-height:22px">genSerial.bat,getSubStr.bat放在</span></span><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px"><span style="line-height:22px; font-size:14px">子目录</span><span style="line-height:22px; font-size:14px"><span style="line-height:22px">lib中。</span></span></span></span>
</div>
<div style="line-height:25px">
<span style="color:#003366; line-height:25px"><span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px"><span style="line-height:22px; font-size:14px"><span style="line-height:22px">关于它们的更多内容请参考《</span></span></span></span><span style="line-height:24px; font-family:Arial,Helvetica,simsun,u5b8bu4f53; white-space:nowrap"><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201132232153255/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none; font-weight:bold">getSubStr</a><span style="line-height:25px">》</span>,<span style="line-height:25px">《</span><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201132224645268/" style="color:rgb(207,121,28); line-height:25px; text-decoration:underline; font-weight:bold">genSerial</a><span style="line-height:25px">》</span></span>
</div>
<div style="line-height:25px">
<span style="line-height:25px; white-space:nowrap">关于更多的Android内存泄露利器请阅读《</span><span style="line-height:24px; font-weight:bold; white-space:nowrap"><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920113252542215/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Android内存泄露利器(RSS内存统计篇)</a></span><span style="line-height:22px; white-space:nowrap">》,《</span><span style="line-height:24px; font-weight:bold; white-space:nowrap"><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920114249163667/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Android内存泄露利器(内存统计篇)</a></span><span style="line-height:22px; white-space:nowrap">》,</span>
</div>
<div style="line-height:25px">
<span style="line-height:22px; white-space:nowrap">《</span><span style="line-height:24px; font-weight:bold; white-space:nowrap"><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792011324114317115/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">Android内存泄露利器(全自动篇)</a></span><span style="line-height:22px; white-space:nowrap">》</span>
</div>
</div>
</div>
</div>
</wbr>
分享到:
相关推荐
Androidstudio生成的hprof文件0Androidstudio生成的hprof文件0
二、Android(Java)中常见的容易引起内存泄漏的不良代码 1 (一) 查询数据库没有关闭游标 2 (二) 构造Adapter时,没有使用缓存的 convertView 3 (三) Bitmap对象不在使用时调用recycle()释放内存 4 (四) 释放对象的...
android app内存泄漏分析工具,用于分析hprof文件
MemoryAnalyzer又名MAT,用于android内存泄漏的hprof文件解析
性能分析工具mat,有效帮助开发者分析内存泄漏,内存抖动等问题
hprof-conv ,内存转mat工具
安卓开发 内存泄露 生成hprof 使用MAT工具分析 很详细 很傻瓜的说明 有图有真相
hprof-conv.exe 关于sdk tools 里面缺少这个运行文件
Android hprof 自动化dump heap脚本 MAC 上执行脚本: pid 为 app 的进程 id sh ./djheap.sh [packageName] [pid] 导出 hprof 文件可以用 mat 工具进行分析
Java内存分析工具MAT(Memory Analyzer Tool) 可以解析内存的消耗,定位具体的类,定位问题
1、内存抖动 2、内存泄漏 3、内存溢出 1、在过程中加上对对象的分析过程 2、解决掉将hprof件映射到内存中的时候可能内存暴涨的问题 4、那么如何定制线上的
该jar包是针对在使用eclipse启动android项目出现如下信息时 DDMS files not found: *\sdk\platform-tools\hprof-conv.exe [2016-07-10 17:09:11 - QtActivity] The connection to adb is down, and a severe ...
hprof-conv.exe 在sdk tools 里面缺少这个运行文件导致DDMS files not found: tools\hprof-conv.exe 错误 , 详细看博客:https://blog.csdn.net/DickyQie/article/details/79657573
java hprof(文件类型分析器)是一款功能强大的java问题解析软件。可以帮助大家在编辑过程种遇到问题的话就马上解决哦。有兴趣的话赶紧下载! 软件介绍: 许多配置属性可以控制分析过程,这些属性也可以通过JobConf...
ScrollTextView的内存文件,用来分析内存泄漏。
解决sdk tools运行缺少adb和hprof-conv文件,解压后直接把这两个文件夹复制到android-sdk-windows目录下,重新运行SDK Manager.exe即可。
下载android-sdk_r23.0.2-linux.tgz tools里有可能缺少hprof-conv