Analyze the cause of memory leak on Android studio 3.0

In the past, when using eclipse, we used DDMS and mat. Not only the steps are complex and cumbersome, but also we have to manually check the location of memory leakage, which is troublesome. Later, with the trend of Android studio, I abandoned eclipse and joined as.

Android studio also began to support automatic memory leak checking, and it is also more convenient to operate.

cover

Poke me to download Android studio 3.0

I don't need a ladder. Will I tell you

1. Write in front

Google released the official version of Android studio 3.0 last week. On the subway at work on Thursday morning, we saw a lot of discussions in the group about various pits of version 3.0. Ah, no, various features. After arriving at the company, we couldn't wait to update version 3.0. Well, it went well. We only met one pit, and everything was going on happy.

What, do you think I want to write what the pit is? Ha ha, I won't tell you, wait... Put down the bricks in your hand and say it later. Today, we mainly talk about how to analyze memory leakage on Android studio 3.0. The content of the article is very simple, but it still takes some time to explore by yourself, so we'll record it here and share it with you.

2. Powerful Android profiler

Click here to view the official documents

In version 3.0, Android uses a new performance analysis tool Android profiler to replace the original Android monitor. The use method is similar to the original. It can analyze the use of CPU, memory and network, but it has a lot of powerful functions.

Start using

I remember that I wrote an article "Android uses rxlifecycle to solve rxjava memory leakage" before. This article will take the demo in this article as an example and use Android studio 3.0 to analyze the memory leakage again.

First, click the profile button in the toolbar to install the app to be analyzed on the device or directly. Select the Android profiler button at the bottom of the as:

Install the app to be analyzed on the equipment

You can see the following prompt, which roughly means that more advanced analysis cannot be performed in the current process:

More advanced analysis cannot be performed in the current process

Click Run configuration to go in and find that the switch cannot be checked, indicating that the version of gradle plug-in is too low and needs to be more than 2.4. Well, update it:

Update gradle plug-in version

It has been updated to version 3.0. You can check the switch and click OK:

Tick switch

Another warning, which probably means that your gradle version has been upgraded to 3.0. It needs to be matched with the 26.0.2 build tool. Listen to you:

Update build tool for version 26.0.2

After the update is completed, you need to run the app again. If you also prompt for more advanced analysis, please restart Android studio. It doesn't matter. You can't use it today anyway. Don't hit me. Let's take a look at the normal Android Profiler:

Android Profiler

Click memory to enter the memory details, where you can view the memory usage in real time:

Memory details

Memory leak analysis

Let's write a program that will leak memory and analyze it:

Very simply, a piece of data is sent every 1s. Because the subscription is not cancelled after closing the activity, rxjava continues to hold the reference of the activity, so the activity will not be recycled during memory recycling, resulting in memory leakage.

Next, repeatedly open and close the page for 5 times, and then manually GC (click the trash can icon in the upper left corner). It is found that the memory occupation has not been reduced:

Memory leak analysis

Analyze the current memory stack (click the icon on the right of the trash can icon):

Analyze memory stack

Select search by package name to find the activity under test. It is found that there are 5 instances. It can be seen that memory leakage has occurred:

Memory leak

Prevent memory leaks

Modify the above code to cancel the subscription when closing the activity:

Repeatedly open the page for 5 times, manually GC, and look at the current stack. You can see that there are no instances of rxlifecyclecomponentsactivity:

No memory leaks

OK, here, I'm finished analyzing memory leakage on Android studio 3.0. Go and try it!

3. Problems encountered in updating Android studio

Error during compilation:

It is found that there is a problem with the code of packaging and outputting APK in gradle. The original code is as follows:

It can be modified as follows:

4. Write at the end

Poke me to download Android studio 3.0

I download the test demo used in this article

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
分享
二维码
< <上一篇
下一篇>>