LeakCanary——直白的展現Android中的記憶體洩露
阿新 • • 發佈:2019-01-09
之前碰到的OOM問題,終於很直白的呈現在我的眼前:我嘗試了MAT,但是發現不怎麼會用。直到今天終於發現了這個新工具:
當我們的App中存在記憶體洩露時會在通知欄彈出通知:
當點選該通知時,會跳轉到具體的頁面,展示出Leak的引用路徑,如下圖所示:
LeakCanary 可以用更加直白的方式將記憶體洩露展現在我們的面前。
android studio (自己弄的)上使用LeakCanary也可以看這個:
工程包括:
- 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><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li></ul>
-
在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><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li></ul>
- 在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>