All Projects → AliAsadi → Avoid Memory Leak Android

AliAsadi / Avoid Memory Leak Android

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

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Avoid Memory Leak Android

Sinsofmemoryleaks
Some common patterns of memory leaks in Android development and how to fix/avoid them
Stars: ✭ 343 (+145%)
Mutual labels:  memory-management, memory-leak, android-development
MemoryPool
simple memory pool / thread safe / minimized context switching / Memory managed in 4 levels / Requirements(Windows xp~ / Visualstudio 2015)
Stars: ✭ 14 (-90%)
Mutual labels:  memory-management, memory-leak
Nevercrash
🌍 全局捕获Crash。信NeverCrash,永不Crash。
Stars: ✭ 170 (+21.43%)
Mutual labels:  thread, handler
poireau
Poireau: a sampling allocation debugger
Stars: ✭ 76 (-45.71%)
Mutual labels:  memory-management, memory-leak
Taskscheduler
A concise,practical async library for Android project,already was used in million devices
Stars: ✭ 385 (+175%)
Mutual labels:  thread, handler
Async Sockets Cpp
Simple thread-based asynchronous TCP & UDP Socket classes in C++.
Stars: ✭ 127 (-9.29%)
Mutual labels:  thread
Bdwgc
The Boehm-Demers-Weiser conservative C/C++ Garbage Collector (libgc, bdwgc, boehm-gc)
Stars: ✭ 1,855 (+1225%)
Mutual labels:  memory-management
Ipyexperiments
jupyter/ipython experiment containers for GPU and general RAM re-use
Stars: ✭ 128 (-8.57%)
Mutual labels:  memory-management
Placepicker
Free Android Map Place Picker alternative using Geocoder instead of Google APIs
Stars: ✭ 126 (-10%)
Mutual labels:  android-development
Unitask
Provides an efficient allocation free async/await integration for Unity.
Stars: ✭ 2,547 (+1719.29%)
Mutual labels:  thread
Ibackdrop
A library to simply use Backdrop in your project (make it easy). Read more ->
Stars: ✭ 137 (-2.14%)
Mutual labels:  android-development
Just Another Android App
An Android base app with loads of cool libraries/configuration NOT MAINTAINED
Stars: ✭ 1,654 (+1081.43%)
Mutual labels:  android-development
Isoalloc
A general purpose memory allocator that implements an isolation security strategy to mitigate memory safety issues while maintaining good performance
Stars: ✭ 130 (-7.14%)
Mutual labels:  memory-management
Uber Car Animation Android
An example project to demonstrate how to Add Uber Like Car Animation in Android App
Stars: ✭ 134 (-4.29%)
Mutual labels:  android-development
Laravel Handlers
Request handlers for Laravel
Stars: ✭ 128 (-8.57%)
Mutual labels:  handler
Simple Dialer
A handy phone call manager with phonebook, number blocking and multi-SIM support
Stars: ✭ 138 (-1.43%)
Mutual labels:  android-development
Backdoor Apk
backdoor-apk is a shell script that simplifies the process of adding a backdoor to any Android APK file. Users of this shell script should have working knowledge of Linux, Bash, Metasploit, Apktool, the Android SDK, smali, etc. This shell script is provided as-is without warranty of any kind and is intended for educational purposes only.
Stars: ✭ 1,766 (+1161.43%)
Mutual labels:  android-development
Chucker
🔎 An HTTP inspector for Android & OkHTTP (like Charles but on device)
Stars: ✭ 2,169 (+1449.29%)
Mutual labels:  android-development
Mmat
An automatically testing and analysis hprof library for android app (自动分析Android内存泄漏)
Stars: ✭ 137 (-2.14%)
Mutual labels:  memory-leak
M5p01 muprokaron
A tiny real-time kernel focusing on formal reliability and simplicity.
Stars: ✭ 132 (-5.71%)
Mutual labels:  thread

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].