How to use mat to analyze memory leaks in Android programs

Source: Internet
Author: User

This article explains how to use the Mat tool to find memory leaks (as an example of androidstudio development tools), using the memory analysis examples in the "Android Development Art Quest" book.

1. Download the mat (Eclipse Memory Analyzer) tool, WINDOWS64 bit network: Http://pan.baidu.com/s/1pLlbOBD, or download it via official website: https://www.eclipse.org /mat/downloads.php, the download is complete after decompression, directory structure as follows:

2, the simulation memory leak scene, the source code is as follows, starts to quit three times the app can.

The code structure is as follows

Mainactivity.java
Package androidstudy.androidartstudy05;

Import Android.content.Context;
Import Android.os.Bundle;
Import Android.os.SystemClock;
Import android.support.v7.app.AppCompatActivity;
Import Android.util.Log;

Import Androidstudy.androidartstudy05.manager.TestManager;

public class MainactivityExtends AppcompatactivityImplements Testmanager.ondataarrivedlistener {
private static Final StringTAG ="Mainactivity";

private static ContextScontext;
@Override
protected voidOnCreate (Bundle savedinstancestate) {
Super.oncreate (Savedinstancestate);
Setcontentview (r.layout.Activity_main);

Scontext =This
Testmanager.GetInstance (). Registerlistener (This;


private synchronized void testanr () {
Systemclock.30 *

private synchronized void initview () {

}

@Override
public void ondataarrived (Object data) {
Log.tag;
} /span>
Testmanager.java
Package Androidstudy.androidartstudy05.manager;

Import Java.util.ArrayList;
Import Java.util.List;

/**
* Created by Administrator on 2016/7/19.
*/
public class Testmanager {
Private list<ondataarrivedlistener>Mondataarrivedlisteners =New Arraylist<ondataarrivedlistener> ();

private Static Class Singletonholder {
public static final TestmanagerINSTANCE =New Testmanager ();
}

Private Testmanager () {
}

public static TestmanagerGetInstance () {
Return Singletonholder.INSTANCE;
}

public synchronized voidregisterlistener (Ondataarrivedlistener listener) {
if (! Mondataarrivedlisteners.contains (Listener)) {
mondataarrivedlisteners.add ( Listener) }
}

public synchronized void unregisterlistener (Ondataarrivedlistener listener) {
Mondataarrivedlisteners.remove (listener) ;


public interface Ondataarrivedlistener {
public void ondataarrived (Object data) ;
} /span>
3. Click the Monitor button on the Androidstudio interface to open the Ddms interface, as shown, click Dump HPROF The file (red box Mark) button exports a hprof suffix androidstudy.androidartstudy05.hprof saved to platform-tools (which is placed in this directory for the next convenience of converting files), my path is e:\ Android\sdk\platform-tools

4, because the exported files can not be directly recognized by the mat, so you need to convert through the hprof-conv command, this command is the tool provided by the Android SDK, located under platform-tools, cmd command switch to this directory after input Hprof-conv Androidstudy.androidartstudy05.hprof androidstudy.androidartstudy05-conv.hprof Command Enter,
A androidstudy.androidartstudy05-conv.hprof file is generated under the Platform-tools directory, such as:

5, open the Mat tool, import the converted file Androidstudy.androidartstudy05-conv.hprof, open after the interface as follows:

6, click "Histogram" button, display the interface as follows, in the red box enter the app's package name androidstudy.androidartstudy05

7, through the results can see the Mainactivity object has three (normal only one), it is obvious that there is a memory leak mainactivity

8. Right-click on the mainactivity "Merge shortest Paths to GC Roots", "Exclude all Phantom/weak/soft etc.refrences".

9, in the open interface can see detailed reference details

10, you can see that there are three references to the Mainactivity object, two for the singleton pair like List (list<ondataarrivedlistener>) attribute mondataarrivedlisteners hold, One for static variables private static Context scontext;



How to use mat to analyze memory leaks in Android programs

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.