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 Handler
Hold externalActivity/Fragment
Object, resulting inActivity/Fragemnt
It wasn’t recycled when it was destroyed -
Solution:
- Change the Handler class to static inner class + weak reference (
WeakReference
) holdActivity
References (Static classes do not hold external references by default) - When an external class (generally
Activity/Fragment
Empties the Handler queue at the end of the lifecycle
- Change the Handler class to static inner class + weak reference (
-
Reason 2:
Instances of non-static inner classes (objects)
=Static instance
(Its life cycle = application life cycle) -
Solution:
- Change this class to a static class (static classes do not hold external references by default)
- 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:
- Change the thread class to a static class (static classes do not hold external references by default)
- 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:
- 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 cursor
Etc. -
Solution:
Activity
Close/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 example
private static Context mContext = context
So the context corresponds to theActivity
It can’t be recycled -
Solution:
- Avoid Static member variable references and use weak references instead of strong ones
- Use the singleton pattern
4. Other uses
-
Cause 1: Context: When it is owned
Activity context
If the object referenced by theActivity
Cannot be recycled when it needs to be destroyed -
Solution:
- right
context
Do not exceed its lifetime - use
ApplicationContext
Instead of
- right
-
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:
- No longer used by multiple process pairs
WebView
Object to be destroyed
- No longer used by multiple process pairs
-
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:
- cached
convertView
- Direct use of
ViewHolder
- cached