How to analyze Android app memory leaks with Mat

Source: Internet
Author: User
Tags php download

Using the tool: Android Studio 2.0 Preview, Android Device Monitor, MAT (Memory Analyzer).

Click "Android Device Monitor" on the Android Studio toolbar, as

Open and select the application process, then click on "Update Heap", then click on each activity of the application repeatedly, the last "Dump HPROF file", as shown in 1-2-3

Save the Hprof file.

The hprof file needs to be converted below.

Open cmd terminal, go to \sdk\platform-tools directory, convert hprof file with hprof-conv command

Hprof-conv f:/a.hprof F:/b.hprof

Finally get the B.hprof file.

To official website: http://www.eclipse.org/mat/downloads.php Download the Memory Analyzer tool.

Choose the version that's right for you.

Open MemoryAnalyzer.exe, select "File", "Open Heap Dump" in the toolbar, select B.hprof

Select the action in the overview and click "Histogram"

You can search for class names in your project and support partial matches. After I enter radar, I match the following content

Right-click one of the, select "Merge shortest Paths to GC Roots", "Exclude all Phantom/weak/soft etc". References "

You can also use the Dominator tree in overview to generate a new list, again using path to GC Roots-exclude Weak/soft references to filter out classes that are associated with program-related memory leaks. Give an example:

Since Toastutil is a tool class, the method is static and accepts the context parameter, at which point the activity context is passed in an activity, Causes Toastutil to hold a reference to the activity and the activity is not released and memory leaks. Solution: Pass a application context in, let this context and toastutil have the same life cycle.

In addition, memory leaks can occur when handler is used improperly, such as using postdelayed in handler.

Common solutions include the following:

1. Using static internal handler/runnable + Weak reference

2. When ondestory, manually clear the message

3. Weakhandler of third parties developed with Badoo

For details, please refer to this article: using Android Studio to analyze memory leaks

How to analyze Android app memory leaks with Mat

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.