memory leaks on android
TRANSCRIPT
![Page 1: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/1.jpg)
Memory Leaks
SOFTWARE ENGINEER | AUTHOR | CONSULTANTOmri Erez
@innovationMaze | https://medium.com/@OomriErez
WHAT YOU DON’T KNOW WILL DRIVE YOU CRAZY
![Page 2: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/2.jpg)
About Me
![Page 3: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/3.jpg)
Memory Leak
A scenario in which our application persistently retains an object’s memory, even after it is not needed anymore.
![Page 5: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/5.jpg)
Component Life Cycle
Activity Life Cycle
App Life Cycle
Service Life Cycle
![Page 6: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/6.jpg)
References TreeGC
Root
O2
O3 O4
![Page 8: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/8.jpg)
GC Roots
Static variables
& functions
References on a stack
JNI references
& objects
![Page 9: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/9.jpg)
A Memory LeakGC
Root
O2
O3
O4
![Page 12: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/12.jpg)
What you don’t know WILL drive you crazy
![Page 13: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/13.jpg)
Scenario 1
![Page 14: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/14.jpg)
private static View mLeakingView; private byte[] mBytes=new byte[1000000]; @Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); mLeakingView = findViewById(R.id.mainView); mLeakingView.setOnClickListener(view -> finish()); MyStringHelper.getInstance().setActivity(this); Log.d(TAG,MyStringHelper.getInstance().getString(R.string.app_name)); }
Static References
![Page 15: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/15.jpg)
Static Referencespublic final class MyStringHelper { private final static MyStringHelper INSTANCE = new MyStringHelper(); private Context mActivityContext; public static MyStringHelper getInstance() {return INSTANCE;} public void setActivity(Context context) {mActivityContext=context;} public String getString(int id) { return mActivityContext.getString(id); } private MyStringHelper() { if (INSTANCE != null) { throw new IllegalStateException("Already instantiated"); } }}
![Page 16: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/16.jpg)
The Source of the Leak
•Static reference to a view
•Static instance of MyStringHelper
Their lifecycle is longer than the activity lifecycle
![Page 17: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/17.jpg)
A Memory Leak
GC RootmLeakingView
Activity
GC RootMyStringHelper
![Page 18: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/18.jpg)
Solution?
![Page 19: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/19.jpg)
Solution :|
@Overrideprotected void onDestroy() { mLeakingView=null; MyStringHelper.getInstance()
.setActivityContext(null); super.onDestroy(); }
![Page 20: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/20.jpg)
Solution :)
•Avoid static references especially to Android components like activities, services and views
•Use Application context instead of the Activity context
![Page 21: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/21.jpg)
Scenario 2
![Page 22: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/22.jpg)
Anonymous Classes//Anonymous classprivate final Handler mLeakyHandler = new Handler() { @Override public void handleMessage(Message msg) { } }; @Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); mView.setOnClickListener(view -> finish()); mLeakyHandler.sendEmptyMessageDelayed(MESSAGE_ID,20000);}
![Page 23: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/23.jpg)
A Memory Leak
Looper ActivitymLeakyHandler
![Page 25: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/25.jpg)
Solution :|
@Overrideprotected void onDestroy() { mLeakyHandler.removeMessages(MESSAGE_ID); super.onDestroy(); }
![Page 26: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/26.jpg)
Solution :)
•When using the Handler class, make sure its static
![Page 27: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/27.jpg)
Scenario 3
![Page 28: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/28.jpg)
Non Static Inner Classes
private class MyRunnable implements Runnable { @Override public void run() { Log.d(TAG, "I am running in activity:" + InnerClassLeakingActivity.this.getComponentName()); } }
![Page 29: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/29.jpg)
Solution :)
•Avoid non-static inner classes
•Use WeakReference if needed
![Page 30: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/30.jpg)
Solution :)private static class MyRunnable implements Runnable{ WeakReference<InnerClassLeakingActivity> mWeakActivity; public MyRunnable(InnerClassLeakingActivity activity) { this.mWeakActivity = new WeakReference<>(activity); } @Override public void run() { final InnerClassLeakingActivity activity=mWeakActivity.get(); if (activity!=null) Log.d(TAG, String.format("I am running in activity:%s" ,activity.getComponentName()));
}}
![Page 31: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/31.jpg)
![Page 32: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/32.jpg)
Tools for Automatic Detection
![Page 33: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/33.jpg)
StrictMode
•Here to help find developers mistakes and bring it to our attention
•Very easy to use
![Page 34: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/34.jpg)
StrictMode :)
if (BuildConfig.DEBUG) { StrictMode.VmPolicy vmPolicy=new StrictMode.VmPolicy.Builder() .detectActivityLeaks()
.detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() .penaltyLog() .build(); StrictMode.setVmPolicy(vmPolicy); }
![Page 35: Memory Leaks on Android](https://reader031.vdocument.in/reader031/viewer/2022022413/58e4a17c1a28aba3458b611b/html5/thumbnails/35.jpg)
Leak Canary
•Created by Pierre-Yves Ricau (Square Inc)
•Very easy to use
•Tracks objects for memory leaks