LeakCanary源码github下载地址:https://github.com/square/leakcanary
之前碰到的OOM问题,终于很直白的呈现在我的眼前:我尝试了MAT,但是发现不怎么会用。直到今天终于发现了这个新工具:
当我们的App中存在内存泄露时会在通知栏弹出通知:
当点击该通知时,会跳转到具体的页面,展示出Leak的引用路径,如下图所示:
LeakCanary 可以用更加直白的方式将内存泄露展现在我们的面前。
以下是我找到的学习资料,写的非常棒:
1、LeakCanary: 让内存泄露无所遁形
2、LeakCanary 中文使用说明
AndroidStudio (官方)上使用LeakCanary 请移步:
https://github.com/square/leakcanary
Eclipse 上使用LeakCanary 请移步我的:
https://github.com/SOFTPOWER1991/LeakcanarySample-Eclipse
android studio (自己弄的)上使用LeakCanary也可以看这个:
leakcanarySample_androidStudio
leakcanary + monkey 检测内存泄露
https://testerhome.com/topics/5944
工程包括:
- LeakCanary库代码
- LeakCanaryDemo示例代码
使用步骤:
-
将LeakCanary import 入自己的工程
-
添加依赖:
compile project(':leakcanary')
-
在Application中进行配置
<code class="hljs java has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExampleApplication</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Application</span> {</span> ...... <span class="hljs-comment">//在自己的Application中添加如下代码</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> RefWatcher <span class="hljs-title">getRefWatcher</span>(Context context) { ExampleApplication application = (ExampleApplication) context .getApplicationContext(); <span class="hljs-keyword">return</span> application.refWatcher; } <span class="hljs-comment">//在自己的Application中添加如下代码</span> <span class="hljs-keyword">private</span> RefWatcher refWatcher; <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span>() { <span class="hljs-keyword">super</span>.onCreate(); ...... <span class="hljs-comment">//在自己的Application中添加如下代码</span> refWatcher = LeakCanary.install(<span class="hljs-keyword">this</span>); ...... } ..... } </code>
-
在Activity中进行配置
<code class="hljs java has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AppCompatActivity</span> {</span> ...... <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) { <span class="hljs-keyword">super</span>.onCreate(savedInstanceState); setContentView(R.layout.activity_main); <span class="hljs-comment">//在自己的应用初始Activity中加入如下两行代码</span> RefWatcher refWatcher = ExampleApplication.getRefWatcher(<span class="hljs-keyword">this</span>); refWatcher.watch(<span class="hljs-keyword">this</span>); textView = (TextView) findViewById(R.id.tv); textView.setOnClickListener(<span class="hljs-keyword">new</span> View.OnClickListener() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onClick</span>(View v) { startAsyncTask(); } }); } <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">async</span>() { startAsyncTask(); } <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">startAsyncTask</span>() { <span class="hljs-comment">// This async task is an anonymous class and therefore has a hidden reference to the outer</span> <span class="hljs-comment">// class MainActivity. If the activity gets destroyed before the task finishes (e.g. rotation),</span> <span class="hljs-comment">// the activity instance will leak.</span> <span class="hljs-keyword">new</span> AsyncTask<Void, Void, Void>() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> Void <span class="hljs-title">doInBackground</span>(Void... params) { <span class="hljs-comment">// Do some slow work in background</span> SystemClock.sleep(<span class="hljs-number">20000</span>); <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>; } }.execute(); } } </code>
- 在AndroidMainfest.xml 中进行配置,添加如下代码
<code class="hljs xml has-numbering"> <span class="hljs-tag"><<span class="hljs-title">service </span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.squareup.leakcanary.internal.HeapAnalyzerService"</span> <span class="hljs-attribute">android:enabled</span>=<span class="hljs-value">"false"</span> <span class="hljs-attribute">android:process</span>=<span class="hljs-value">":leakcanary"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">service </span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.squareup.leakcanary.DisplayLeakService"</span> <span class="hljs-attribute">android:enabled</span>=<span class="hljs-value">"false"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">activity </span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.squareup.leakcanary.internal.DisplayLeakActivity"</span> <span class="hljs-attribute">android:enabled</span>=<span class="hljs-value">"false"</span> <span class="hljs-attribute">android:icon</span>=<span class="hljs-value">"@drawable/__leak_canary_icon"</span> <span class="hljs-attribute">android:label</span>=<span class="hljs-value">"@string/__leak_canary_display_activity_label"</span> <span class="hljs-attribute">android:taskAffinity</span>=<span class="hljs-value">"com.squareup.leakcanary"</span> <span class="hljs-attribute">android:theme</span>=<span class="hljs-value">"@style/__LeakCanary.Base"</span> ></span> <span class="hljs-tag"><<span class="hljs-title">intent-filter</span>></span> <span class="hljs-tag"><<span class="hljs-title">action</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.intent.action.MAIN"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">category</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.intent.category.LAUNCHER"</span> /></span> <span class="hljs-tag"></<span class="hljs-title">intent-filter</span>></span> <span class="hljs-tag"></<span class="hljs-title">activity</span>></span> </code><pre name="code" class="prettyprint"><code class="hljs xml has-numbering"></code><h4 id="配置leakcanary"><pre name="code" class="prettyprint"><code class="hljs xml has-numbering">monkey结合</code>