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
What are the conditions of memory leakage and the corresponding 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 through multithreading
WebView
Object to be destroyed
- No longer used through multithreading
-
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
Finally: Learning summary — Android Framework Architecture knowledge brain map (purely hand-drawn XMIND document)
After learning, if you want to check the effect of how, in fact, the best method is to sum up their own. For example, AFTER learning something, I will hand-draw an outline brain map of xMind file, which is convenient for subsequent review and my own understanding. I believe that I can quickly complete the knowledge by glancing at it and imagine it back.
The following is my hand-drawn Brain map of Android framework architecture knowledge. Since it is an Xmind file, it is not easy to upload, so the editor has exported it here in the form of pictures. The details are not particularly clear. But to interested friends can provide a complete Android framework architecture knowledge brain map original (including the above interview analysis Xmind document)
In addition, the above mentioned handwritten documents of Android framework architecture of Alibaba collector edition and a book of “Big Talk Data Structure” and other related study notes can also be shared with approved friends!
— Thanks for Free Download! 【Git】