Detailed explanation of memory leakage in Android performance optimization

overview

Memory leak refers to the failure of a program to release memory that is no longer used due to negligence or error. In Android, when an object holds a reference to an activity, if the object cannot be recycled by the system, the activity will not be recycled by the system when the activity is no longer used, so there is a memory leak. Memory leakage once or twice in the application will not have any impact on the acquisition, but after the application is used for a long time, if there are a large number of activities that cannot be recycled by GC, it will eventually lead to the emergence of oom. So let's analyze the common factors leading to memory leakage and how to detect memory leakage.

Common causes of memory leaks

Scenario 1: static activity and view

Static variables activity and view will lead to memory leakage. In the following code, set the context and textview of activity as static objects, resulting in memory leakage.

Scenario 2: thread, anonymous class, inner class

In the following code, there is a non static anonymous class object thread, which will implicitly hold the reference leakactivity of an external class, resulting in memory leakage. Similarly, if the thread is used as the inner class of leakactivity instead of the anonymous inner class, it will also hold the reference of the outer class, resulting in memory leakage. Here, you only need to define the anonymous class for thread as a static internal class (the static internal class will not hold an implicit reference to the external class).

Scenario 3: Animation

There is a kind of infinite loop animation in the attribute animation. If you play this kind of animation in the activity and stop the animation in ondestroy, the animation will continue to play. At this time, the activity will be held by the view, so that the activity cannot be released. To solve this problem, ondestroy in the activity needs to call objectanimator. Cancel() to stop the animation.

Scenario 4: handler

The memory leakage of the handler lies in the message mechanism of Android - the working process of the handler

Scenario 5: improper use of third-party library

For the use of some third open source frameworks such as eventbus and rxjava, if you do not unsubscribe before the activity is destroyed, it will lead to memory leakage.

Using mat to detect memory leaks

After introducing common memory leaks, let's take a look at using mat (memory analysis tool) to detect memory leaks. The download address of mat is: http://www.eclipse.org/mat/downloads.php 。

Let's look at an error code that can lead to memory leakage.

In the above code, there is a memory leak because eventbus does not unregister. Let's take this code as an example to see how to analyze memory leakage.

Open monitors in Android studio to see the following interface.

It can be seen here that when the application is just started, the memory occupied is 15m. Then we start to operate the app and repeatedly enter and exit leakacticity. Click the GC button as shown in. At this time, we are looking at the memory usage.

Here we can see that the memory has been increasing continuously, reaching 33m, and can not be recycled by GC. Therefore, we can judge that there must be a memory leak at this time. Now click the dump Java heap button to see the generated hprof file in the captures window. However, the generated hprof file is not in the standard format. We need to convert it through the tool hprof conv provided by the SDK, which is in the platform Tools Directory of the SDK. Execute the following command:

hprof-conv XXX.hprof converted-dump.hprof

Of course, this step can be omitted in Android studio. You can directly export hprof files in standard format.

At this time, you can open the exported hprof file through the mat tool. The opening interface is shown in the following figure:

The most commonly used in mat are histogram and domino tree, which correspond to buttons a and B in the figure above respectively. Histogram can see the number of different types of buffers in memory and the size of memory occupied, while domino tree sorts the objects in memory in order from large to small, and can analyze the reference relationship between objects. Here we will introduce the meaning of the two symbols in mat.

Histogram

Since most memory leaks in Android occur in activity, you can click the histogram button and search for activity.

It can be seen here that there are 69 objects in leakactivity. Basically, it can be concluded that there is a memory leak. At this time, you can analyze it by viewing the reference chain of GC objects. Right click to select merge shortest paths to GC roots and select exclude weak / soft references to exclude weak and soft references.

After excluding soft references and weak references, see the following figure:

Here you can see the leakactivity memory leak caused by eventbus.

In histogram, you can also see that an object contains references to those objects. For example, to view the references contained in leakactivity, right-click and select with incoming reference in list objects. With outgoing reference means that the selected object holds references to those objects. Write a picture description here

Dominator Tree

Now we can click the domino tree button and search for activities. You can see the following figure:

Here you can see that there are a large number of leakactivities. Then right-click and select path to GC roots - > exclude weak / soft references to exclude weak and soft references.

After that, you can see the following results. It is still the memory leak caused by eventbus:

summary

Memory leaks are often ignored, but when a large number of memory leaks lead to oom. Its impact should not be underestimated. Of course, in addition to the above analysis of memory leakage, I think we can also analyze memory leakage through leakcanary. The use of leakcanary is not described in detail here.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>