LeakCanary——直白的展现Android中的内存泄露
2017-02-23 09:48
363 查看
当我们的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库代码
LeakCanaryDemo示例代码
使用步骤:
将LeakCanary import 入自己的工程
添加依赖:
在Application中进行配置
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
在Activity中进行配置
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
在AndroidMainfest.xml 中进行配置,添加如下代码
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
当点击该通知时,会跳转到具体的页面,展示出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库代码
LeakCanaryDemo示例代码
使用步骤:
将LeakCanary import 入自己的工程
添加依赖:
compile project(':leakcanary')
在Application中进行配置
public class ExampleApplication extends Application { ...... //在自己的Application中添加如下代码 public static RefWatcher getRefWatcher(Context context) { ExampleApplication application = (ExampleApplication) context .getApplicationContext(); return application.refWatcher; } //在自己的Application中添加如下代码 private RefWatcher refWatcher; @Override public void onCreate() { super.onCreate(); ...... //在自己的Application中添加如下代码 refWatcher = LeakCanary.install(this); ...... } ..... }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
在Activity中进行配置
public class MainActivity extends AppCompatActivity { ...... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //在自己的应用初始Activity中加入如下两行代码 RefWatcher refWatcher = ExampleApplication.getRefWatcher(this); refWatcher.watch(this); textView = (TextView) findViewById(R.id.tv); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startAsyncTask(); } }); } private void async() { startAsyncTask(); } private void startAsyncTask() { // This async task is an anonymous class and therefore has a hidden reference to the outer // class MainActivity. If the activity gets destroyed before the task finishes (e.g. rotation), // the activity instance will leak. new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { // Do some slow work in background SystemClock.sleep(20000); return null; } }.execute(); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
在AndroidMainfest.xml 中进行配置,添加如下代码
<service android:name="com.squareup.leakcanary.internal.HeapAnalyzerService" android:enabled="false" android:process=":leakcanary" /> <service android:name="com.squareup.leakcanary.DisplayLeakService" android:enabled="false" /> <activity android:name="com.squareup.leakcanary.internal.DisplayLeakActivity" android:enabled="false" android:icon="@drawable/__leak_canary_icon" android:label="@string/__leak_canary_display_activity_label" android:taskAffinity="com.squareup.leakcanary" android:theme="@style/__LeakCanary.Base" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
相关文章推荐
- LeakCanary——直白的展现Android中的内存泄露
- LeakCanary——直白的展现Android中的内存泄露
- LeakCanary——直白的展现Android中的内存泄露
- LeakCanary——直白的展现Android中的内存泄露
- LeakCanary——直白的展现Android中的内存泄露
- LeakCanary——直白的展现Android中的内存泄露
- LeakCanary——直白的展现Android中的内存泄露
- LeakCanary——直白的展现Android中的内存泄露
- Android内存优化8 内存检测工具2 LeakCanary——直白的展现Android中的内存泄露
- LeakCanary——直白的展现Android中的内存泄露
- LeakCanary——直白的展现Android中的内存泄露
- 56、LeakCanary——直白的展现Android中的内存泄露
- LeakCanary(一)直白的展现Android中的内存泄露
- android 中的内存泄露检测方案LEAKCANARY远离OOM
- WebView与static引用内存泄露(LeakCanary)-android
- Android 源码系列之<十四>从源码的角度深入理解LeakCanary的内存泄露检测机制(下)
- Android内存泄露自动检测神器LeakCanary
- LeakCanary——消除Android中的内存泄露
- Android内存泄露自动检测LeakCanary
- Android 源码系列之<十三>从源码的角度深入理解LeakCanary的内存泄露检测机制(中)