`
mmdev
  • 浏览: 12931636 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Android内存泄露利器(hprof篇)

 
阅读更多
我们可以使用代码生成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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics