catch leak if you can

Post on 14-Apr-2017

131 Views

Category:

Mobile

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CATCH LEAKIF YOU CAN

PAULINA SZKLARSKA

HELLO!

Android Developer in DORfor 2 years

paulina.szklarska@droidsonroids.plhttps://github.com/pszklarska

“A small leak will sink a great ship

~ Benjamin Franklin

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

MEMORY LEAKS

1.WHAT ARE MEMORY LEAKS?

WHAT IS MEMORY LEAK?

NEW DELETE

NEW DELETE

C++

JAVA

WHAT IS MEMORY LEAK?

NEW OBJECTS

MEMORY

OBJECT A

OBJECT BOBJECT C

WHAT IS MEMORY LEAK?

NEW OBJECTS

MEMORY

OBJECT A

OBJECT C

GC

WHAT IS MEMORY LEAK?

NEW OBJECTS

MEMORY

OBJECT A

OBJECT C

GC

2.WHEN DO WE HAVE MEMORY LEAKS?

LeakExample

LeakExample

LeakExample has stopped!

3.HOW CAN WE CATCH MEMORY LEAKS?

ANDROID MONITOR

ANDROID MONITOR

ANDROID MONITOR

ANDROID MONITOR

ANDROID MONITOR

HEAP VIEWER

HEAP VIEWER

MEMORY ANALYZER TOOL

MEMORY ANALYZER TOOL

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

▸ allows to read HPROF files

MEMORY ANALYZER TOOL

1.GET HPROF FILE FROM ANDROID STUDIO

1.

2.

REFERENCES▸ strong

Button button = new Button(context); ▸ weak

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

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

▸ phantom

4.WHAT MAY CAUSE MEMORY LEAKS?

POSSIBLE CAUSES

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

1.STATIC CONTEXT

1.STATIC CONTEXT

▸ don’t use static Context

▸ try to use Application-Context first

1.STATIC CONTEXT

2. INNER CLASSES

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

2. INNER CLASSES

3. ANONYMOUS CLASSES

4. RXJAVA

4. RXJAVA

5. SYSTEM SERVICES

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

5. SYSTEM SERVICES

5. SYSTEM SERVICES

5.OTHER WAYS FOR MEMORY LEAKS

LEAK CANARY

SOURCE: https://github.com/

square/leakcanary

LEAK CANARY - REFWATCHER

LEAK CANARY - REFWATCHER

LEAK CANARY - REFWATCHER

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

THANKS!Any questions?You can find me at paulina.szklarska@droidsonroids.pl

top related