Use Android Studio to detect memory leaks and resolve memory leak issues

Source: Internet
Author: User

  Since Google released Android studio in 2013, Android studio has been replacing eclipse with its own good memory optimizations, cool UI themes, powerful auto-completion hints, and Gradle compiler support. Become the mainstream Android development IDE. Android Studio provides us with a good coding experience, as well as many tools for app performance analysis, making it easier for developers to analyze app performance. Google has been telling developers in the IO conference not to use cell phone memory sparingly, and beware of bad development practices that can lead to memory leaks in the app. Although the online detection of the app memory leaks article voluminous, but to use DDMS and mat, not only the use of complicated steps, but also to manually troubleshoot the location of memory leaks, operation is more inconvenient. In fact, Android Studio has started to support the automatic memory leak check, this article with everyone to explore the mystery of it.


What is a memory leak
  This is also a regular interview, in Layman's terms, the defined variables are not used, that is, memory leaks. The garbage collection of Android virtual machines is based on the root search algorithm and the program counter algorithm. The GC will traverse the heap starting from the root node (GC Roots). In the end, some of the garbage that is not directly or indirectly referenced to GC roots is reclaimed by GC. The reason for the memory leak is that there is an invalid reference, causing the object that would otherwise need to be GC not to be recycled.


Give me a chestnut.

Mleak is a static variable stored in a static zone, while leak is an inner class that holds a reference to the external class activity. This causes the activity to be destroyed, because it is held by the Mleak, so the system does not GC it, resulting in a memory leak.


And one of the most frequently made chestnuts.

  If we pass in the activity when we call Singleton's getinstance () method. The activity will persist when the instance is not released. Thus causing a memory leak.
The workaround is to change the new Singleton (context) to New Singleton (Context.getapplicationcontext ()) so that it doesn't matter with the incoming activity.


Detection of memory leaks
  Open Android Studio, compile the code, run the app on the emulator or the real machine, then click on the Monitor tab under Android Monitor to enter the following screen

 In the memory column, you can observe the dynamic use of different times of the app, click can manually trigger GC, click to enter the Hprof viewer interface, view Java heap, such as

  The Reference tree represents a reference to the instance from which you can see the cause of the memory leak, shallow size refers to the amount of memory that the object itself occupies, and retained size represents the sum of memory that the garbage collector can reclaim after the object is freed.
Here we take the palm of the city client as an example, to explore the method of memory leak detection.
Open Android Studio, compile the code, run the palm of the city, and start playing with our app, then watch the app's memory usage curve from within memory monitor and suddenly find that Nani!!! How memory usage is getting bigger, this is probably a memory leak, then click on the Manual GC, then click to watch Javaheap, click on the Analyzer task,android Monitor can automatically analyze the leaked activity for us, Analyze it as shown

  In the reference tree, we can directly see the singleton object that holds the activity, navigate directly to the code in that single example, and discover that the code appears

  just the same as the mistakes that were raised in the chestnuts, We fixed the problem of checking out the memory leaks and ran the pre-and post-repair code on the same emulator and performed the same operation to see how they used the memory, as shown in

Memory leak, memory consumption is about 43M

fixed a memory leak with 36M of memorymemory usage drops after fixing a memory leak problem 16.3%!!! Having mastered the way Android monitor is used, I believe I can help you on the way to Android development.

Use Android Studio to detect memory leaks and resolve memory leak issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.