What is a memory leak

Memory leakML (Memory Leak)Memory is memory that cannot be released and returned to a program when it is no longer needed

Causes of memory leaks and solutions

The root cause of a memory leak is when the memory that needs to be reclaimed is held by another variable reference, resulting in memory reclamation failure

Common reasons are:

1. Non-static inner class/anonymous class

  • Reason 1:Non-static inner class/anonymous class HandlerHold externalActivity/FragmentObject, resulting inActivity/FragemntIt wasn’t recycled when it was destroyed

  • Solution:

    1. Change the Handler class to static inner class + weak reference (WeakReference) holdActivityReferences (Static classes do not hold external references by default)
    2. When an external class (generallyActivity/FragmentEmpties the Handler queue at the end of the lifecycle
  • Reason 2:Instances of non-static inner classes (objects) = Static instance(Its life cycle = application life cycle)

  • Solution:

    1. Change this class to a static class (static classes do not hold external references by default)
    2. Extract the class and encapsulate it as a singleton
  • Cause 3: The external class needs to be destroyed while the multithreaded worker thread is working on a task. The worker thread holds a reference to the external class and cannot be reclaimed

  • Solution:

    1. Change the thread class to a static class (static classes do not hold external references by default)
    2. Force the thread to terminate when the external class ends its life cycle

2. Collection classes

  • Cause: The collection class holds a reference to the collection element after it is added, making the collection element unrecyclable and causing a memory leak

  • Solution:

    1. After the collection class has finished using an element object, it must remove that element from the collection (since there are multiple elements in a collection, the simplest solution is to clean the collection object (CLEAN) & set to Null)

3. The resource object is not closed after being used

  • Cause: Resource objects are not closed after use, and resources are not closed/unlogged during Activity/Fragemnt destruction, which will result in unrecyclable resources. For example:Radio BraodcastReceiver,File stream Fire,Image resource Bitmap,Database cursorEtc.

  • Solution:

    1. ActivityClose/deregister resources in time for destruction

4. The Static keyword decorates a member variable

  • Reason: The Static keyword modifies the life cycle of a member variable = the life cycle of an application for exampleprivate static Context mContext = contextSo the context corresponds to theActivityIt can’t be recycled

  • Solution:

    1. Avoid Static member variable references and use weak references instead of strong ones
    2. Use the singleton pattern

4. Other uses

  • Cause 1: Context: When it is ownedActivity contextIf the object referenced by theActivityCannot be recycled when it needs to be destroyed

  • Solution:

    1. rightcontextDo not exceed its lifetime
    2. useApplicationContextInstead of
  • Cause 2: WebView: After the WebView object is no longer used, it is not destroyed in time. As a result, the memory is occupied

  • Solution:

    1. No longer used by multiple process pairsWebViewObject to be destroyed
  • Cause 3: When The Adapter slides ListView and RecycleView to obtain a new View, it re-instantiates a View object in getView(), which wastes resources and occupies more and more memory

  • Solution:

    1. cachedconvertView
    2. Direct use ofViewHolder