AliAsadi / avoid-memory-leak-android

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

avoid-memory-leak-android

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
public static Context context;

public SampleClass(Activity activity) {
    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
public class MainActivity extends Activity {

    private class DownloadTask extends Thread {
        //do some work 
    }
}
  1. Avoid strong reference use WeakReference for listeners.
public class DownloadTask extends AsyncTask<Void, Void, Void> {

    private WeakReference<DownloadListener> listener;

    public DownloadTask(DownloadListener listener) {
        listener = new WeakReference<>(listener);
    }

    @Override
    protected Void doInBackground(Void... params) {
       ///do some work
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        if (listener.get() != null) {
            listener.get().onDownloadTaskDone();
        }
    }
}
}
  1. Clean/Stop all your handlers, animation listeners onDestroy()/onStop().
protected void onStop() {
    super.onStop();
    handler.clearAllMessages();
    unregisterReceivers();
    view = null;
    listener = null;
}
  1. Avoid Auto-Boxing
public Integer autoBoxing(){
    Integer result = 5;
    return result;
}
public Integer hiddenAutoBoxing(){
    return 5;
}

How to avoid Auto-Boxing:

public int autoBoxing(){
    int result = 5;
    return result;
}
public int hiddenAutoBoxing(){
    return 5;
}
  1. Avoid Auto-Boxing in HashMap - Use SparseArray insead.
public Integer hiddenAutoBoxing(){
    HashMap<Integer, String> hashMap = new HashMap<>();
    hashMap.put(5,"Hi Android Academy");
}

How to avoid Auto-Boxing in HashMap:

public Integer noKeyAutoBoxing(){
    SparseArray<String> sparseArray = new SparseArray<>();
    sparseArray.put(5,"Hi Android Academy");
}
public Integer noValueAutoBoxing(){
    SparseIntArray sparseArray = new SparseIntArray();
    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


Languages

Language:Java 100.0%