catch leak if you can

55
CATCH LEAK IF YOU CAN PAULINA SZKLARSKA

Upload: paulina-szklarska

Post on 14-Apr-2017

130 views

Category:

Mobile


0 download

TRANSCRIPT

Page 1: Catch Leak If You Can

CATCH LEAKIF YOU CAN

PAULINA SZKLARSKA

Page 2: Catch Leak If You Can

HELLO!

Android Developer in DORfor 2 years

[email protected]://github.com/pszklarska

Page 3: Catch Leak If You Can

“A small leak will sink a great ship

~ Benjamin Franklin

Page 4: Catch Leak If You Can

AGENDA1. WHAT ARE… 2. WHEN DO WE HAVE… 3. HOW CAN WE CATCH… 4. WHAT MAY CAUSE… 5. OTHER WAYS FOR…

MEMORY LEAKS

Page 5: Catch Leak If You Can

1.WHAT ARE MEMORY LEAKS?

Page 6: Catch Leak If You Can

WHAT IS MEMORY LEAK?

NEW DELETE

NEW DELETE

C++

JAVA

Page 7: Catch Leak If You Can

WHAT IS MEMORY LEAK?

NEW OBJECTS

MEMORY

OBJECT A

OBJECT BOBJECT C

Page 8: Catch Leak If You Can

WHAT IS MEMORY LEAK?

NEW OBJECTS

MEMORY

OBJECT A

OBJECT C

GC

Page 9: Catch Leak If You Can

WHAT IS MEMORY LEAK?

NEW OBJECTS

MEMORY

OBJECT A

OBJECT C

GC

Page 10: Catch Leak If You Can

2.WHEN DO WE HAVE MEMORY LEAKS?

Page 11: Catch Leak If You Can

LeakExample

Page 12: Catch Leak If You Can

LeakExample

Page 13: Catch Leak If You Can

LeakExample has stopped!

Page 14: Catch Leak If You Can
Page 15: Catch Leak If You Can
Page 16: Catch Leak If You Can

3.HOW CAN WE CATCH MEMORY LEAKS?

Page 17: Catch Leak If You Can

ANDROID MONITOR

Page 18: Catch Leak If You Can
Page 19: Catch Leak If You Can

ANDROID MONITOR

Page 20: Catch Leak If You Can

ANDROID MONITOR

Page 21: Catch Leak If You Can

ANDROID MONITOR

Page 22: Catch Leak If You Can

ANDROID MONITOR

Page 23: Catch Leak If You Can
Page 24: Catch Leak If You Can

HEAP VIEWER

Page 25: Catch Leak If You Can

HEAP VIEWER

Page 26: Catch Leak If You Can
Page 27: Catch Leak If You Can

MEMORY ANALYZER TOOL

Page 28: Catch Leak If You Can

MEMORY ANALYZER TOOL

▸ works as a part of Eclipse or as a standalone app

▸ allows to read HPROF files

Page 29: Catch Leak If You Can

MEMORY ANALYZER TOOL

1.GET HPROF FILE FROM ANDROID STUDIO

Page 30: Catch Leak If You Can

1.

2.

Page 31: Catch Leak If You Can
Page 32: Catch Leak If You Can

REFERENCES▸ strong

Button button = new Button(context); ▸ weak

WeakReference<Activity> activityRef = new WeakReference(mainActivity);

▸ softSoftReference<View> viewRef = new SoftReference(view);

▸ phantom

Page 33: Catch Leak If You Can
Page 34: Catch Leak If You Can
Page 35: Catch Leak If You Can

4.WHAT MAY CAUSE MEMORY LEAKS?

Page 36: Catch Leak If You Can

POSSIBLE CAUSES

1.Static Context (Activity, View)2.Inner Classes3.Anonymous Classes4.RxJava5.System Services

Page 37: Catch Leak If You Can

1.STATIC CONTEXT

Page 38: Catch Leak If You Can

1.STATIC CONTEXT

▸ don’t use static Context

▸ try to use Application-Context first

Page 39: Catch Leak If You Can

1.STATIC CONTEXT

Page 40: Catch Leak If You Can

2. INNER CLASSES

Page 41: Catch Leak If You Can

2. INNER CLASSES

▸ don’t make non-static inner classes that may live longer than Activity

▸ don’t keep reference to Activity by something that may live longer than Activity

▸ try to replace non-static inner class with static inner class

Page 42: Catch Leak If You Can

2. INNER CLASSES

Page 43: Catch Leak If You Can

3. ANONYMOUS CLASSES

Page 44: Catch Leak If You Can

4. RXJAVA

Page 45: Catch Leak If You Can

4. RXJAVA

Page 46: Catch Leak If You Can

5. SYSTEM SERVICES

Page 47: Catch Leak If You Can

▸ remember to unregister all active listeners when they’re unneeded(not only system services)

5. SYSTEM SERVICES

Page 48: Catch Leak If You Can

5. SYSTEM SERVICES

Page 49: Catch Leak If You Can

5.OTHER WAYS FOR MEMORY LEAKS

Page 50: Catch Leak If You Can

LEAK CANARY

SOURCE: https://github.com/

square/leakcanary

Page 51: Catch Leak If You Can

LEAK CANARY - REFWATCHER

Page 52: Catch Leak If You Can

LEAK CANARY - REFWATCHER

Page 53: Catch Leak If You Can

LEAK CANARY - REFWATCHER

Page 54: Catch Leak If You Can

LINKS▸ Memory Monitor

https://developer.android.com/studio/profile/am-memory.html▸ Heap Viewer

https://developer.android.com/studio/profile/am-hprof.html▸ Memory Analyzer Tool

http://www.eclipse.org/mat/▸ Performance Tips

https://developer.android.com/training/articles/perf-tips.html▸ How to analyze Heap Dumps?

http://docwiki.cisco.com/wiki/How_to_analyze_heap_dumps

Page 55: Catch Leak If You Can

THANKS!Any questions?You can find me at [email protected]