Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🔥 Examples of memory leaks and common patterns that cause them in Android development and how to fix/avoid them

NotificationsYou must be signed in to change notification settings

AliAsadi/avoid-memory-leak-android

Repository files navigation

This project is all about shows common patterns of memory leaks in Android development and how to fix them

Android Arsenal

There is 2 seperated modules:

  1. leak-app-> Describe and shows how to cause a leak when we use AsyncTask, Handler, Singleton, Thread.

  2. 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.

Screenshot

How To Avoid Memory Leak?

  1. Do not keep long-lived references to a context-activity
publicstaticContextcontext;publicSampleClass(Activityactivity) {context = (Context)activity;}
  1. Try using the context-application instead of a context-activity
Utils.doSomeLongRunningTask(getApplicationContext());SingletoneManager.getInstance(getApplicationContext());
  1. Avoid non-static inner classes
publicclassMainActivityextendsActivity {privateclassDownloadTaskextendsThread {//do some work    }}
  1. 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();        }    }}}
  1. Clean/Stop all your handlers, animation listeners onDestroy()/onStop().
protectedvoidonStop() {super.onStop();handler.clearAllMessages();unregisterReceivers();view =null;listener =null;}
  1. Avoid Auto-Boxing
publicIntegerautoBoxing(){Integerresult =5;returnresult;}
publicIntegerhiddenAutoBoxing(){return5;}

How to avoid Auto-Boxing:

publicintautoBoxing(){intresult =5;returnresult;}
publicinthiddenAutoBoxing(){return5;}
  1. Avoid Auto-Boxing in HashMap - Use SparseArray insead.
publicIntegerhiddenAutoBoxing(){HashMap<Integer,String>hashMap =newHashMap<>();hashMap.put(5,"Hi Android Academy");}

How to avoid Auto-Boxing in HashMap:

publicIntegernoKeyAutoBoxing(){SparseArray<String>sparseArray =newSparseArray<>();sparseArray.put(5,"Hi Android Academy");}
publicIntegernoValueAutoBoxing(){SparseIntArraysparseArray =newSparseIntArray();sparseArray.put(5,1000);}

Tools which can help you identify leaks

  • 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

License

   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

Stars

Watchers

Forks

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp