Android memory leak causes and solutions (with LeakCanary diagram)

reason

There are two main reasons for memory leaks in Android:

1. Non-static inner classes hold references to outer classes by default

For example, we are often used to directly confronting an interface or abstract class in a class, or overriding a method in a class (override).
These situations are called anonymous inner classes, and anonymous inner classes hold the outer class by default. a citation .
That is to say, the instance (that is, the object) of the non-static inner class will automatically generate a member variable (that is, the attribute defined by the class), and this member variable will automatically point to the instance of the outer class.

2. Static Objects

For example, in the singleton pattern we often encounter, the activity object is passed in the constructor of the singleton pattern, so that the singleton object that exists in the entire app life cycle has always held a reference to this activity, resulting in this activity cannot be used by the system. Recycle.

talk about citations

The interaction between the various objects of the class will inevitably lead to mutual references.
Holding a reference to an object generally occurs in the following scenarios:
1. Declare an inner class
An inner class will have a member variable (that is, an instance variable) by default, and the object type is an outer class, that is, an attribute of an outer class object ;
2. Passing parameters Pass
the referenced object in the form of parameters to the object to be referenced, such as through a function (constructor, ordinary function);
3. Data
member Pass the reference of the referenced object as the object to be referenced A data member in , that is, a member variable (ie instance variable)
of the object that needs to be referenced; in this way, the object that needs to be referenced can operate on the members of the referenced object.
for example

public class Demo {
private A a;
    ......
}

The member variable of the Demo class here points to the instance of A, which causes the Demo object to generate a reference to the A object.

solution

To use tools, LeakCanary is recommended here.

LeakCanary

This tool is the best choice for detecting Android memory leaks.
write picture description here
A few things to note when reading LeakCanary's logs:

1. Sequence

From top to bottom, the top represents the starting point of the reference, then the reference is layer by layer, and the last layer represents the leaked object.
as the picture shows.

2. Grammar

 Object$2.this$0

Represents the second anonymous inner class in the Object class, a member variable (this$0) in this inner class;
this member variable holds a reference.

Object$2.val$request

Indicates the second anonymous inner class in the Object class, the temporary variable val declared outside the anonymous inner class, and the variable name is request;
generally, this variable is a parameter of the function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325161943&siteId=291194637