- Notifications
You must be signed in to change notification settings - Fork31
AliAsadi/avoid-memory-leak-android
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This project is all about shows common patterns of memory leaks in Android development and how to fix them
leak-app-> Describe and shows how to cause a leak when we use AsyncTask, Handler, Singleton, Thread.
fixed-app-> Describe and shows how to avoid/fix the leaks
In Android Studio choose which project you want to run on the top bar.
- Do not keep long-lived references to a context-activity
publicstaticContextcontext;publicSampleClass(Activityactivity) {context = (Context)activity;}
- Try using the context-application instead of a context-activity
Utils.doSomeLongRunningTask(getApplicationContext());SingletoneManager.getInstance(getApplicationContext());
- Avoid non-static inner classes
publicclassMainActivityextendsActivity {privateclassDownloadTaskextendsThread {//do some work }}
- Avoid strong reference use WeakReference for listeners.
publicclassDownloadTaskextendsAsyncTask<Void,Void,Void> {privateWeakReference<DownloadListener>listener;publicDownloadTask(DownloadListenerlistener) {listener =newWeakReference<>(listener); }@OverrideprotectedVoiddoInBackground(Void...params) {///do some work }@OverrideprotectedvoidonPostExecute(VoidaVoid) {super.onPostExecute(aVoid);if (listener.get() !=null) {listener.get().onDownloadTaskDone(); } }}}
- Clean/Stop all your handlers, animation listeners onDestroy()/onStop().
protectedvoidonStop() {super.onStop();handler.clearAllMessages();unregisterReceivers();view =null;listener =null;}
- Avoid Auto-Boxing
publicIntegerautoBoxing(){Integerresult =5;returnresult;}
publicIntegerhiddenAutoBoxing(){return5;}
publicintautoBoxing(){intresult =5;returnresult;}
publicinthiddenAutoBoxing(){return5;}
- Avoid Auto-Boxing in HashMap - Use SparseArray insead.
publicIntegerhiddenAutoBoxing(){HashMap<Integer,String>hashMap =newHashMap<>();hashMap.put(5,"Hi Android Academy");}
publicIntegernoKeyAutoBoxing(){SparseArray<String>sparseArray =newSparseArray<>();sparseArray.put(5,"Hi Android Academy");}
publicIntegernoValueAutoBoxing(){SparseIntArraysparseArray =newSparseIntArray();sparseArray.put(5,1000);}
- LeakCanary from Square is a good tool for detecting memory leaks in your app
- Profiler View the Java heap and memory allocations with Memory Profiler
Copyright (C) 2018 Ali Asadi Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.About
🔥 Examples of memory leaks and common patterns that cause them in Android development and how to fix/avoid them
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Contributors2
Uh oh!
There was an error while loading.Please reload this page.






