[Android] Generate heap dump file (. hprof)

Source: Internet
Author: User


Android generates a heap dump file(. hprof)

A heap dump is a snapshot of the program heap that tells you which parts of the program are using most of the memory.

It is stored in a binary format called Hprof. For files generated after Android executes the Android.os.Debug.dumpHprofData (Hprofpath) method, the. hprof file needs to be converted from the Dalvik format to the J2SE hprof format. The conversion operation is performed using the Hprof-conv tool provided by ANDROIDSDK.

Hprof-conv dump.hprof Converted-dump.hprof

This article belongs to Sodino original, posted in Blog:Http://blog.csdn.net/sodino, reprint please indicate the source.

The relevant code can be downloaded from the group file in QQ Group Code2share (363267446).

The Android code generates a dump file as follows:

    public static Boolean Createdumpfile (Contextcontext) {Stringlog_path = "/dump.gc/";        boolean bool = false;        SIMPLEDATEFORMATSDF = new SimpleDateFormat ("Yyyy-mm-dd_hh.mm.ssss");        Stringcreatetime = Sdf.format (New Date (System.currenttimemillis ()));        Stringstate = Android.os.Environment.getExternalStorageState (); Determines whether the sdcard exists and is available if (Android.os.Environment.MEDIA_MOUNTED.equals (state)) {filefile = new File (Enviro            Nment.getexternalstoragedirectory (). GetPath () +log_path);            if (!file.exists ()) {file.mkdirs ();            } Stringhprofpath = File.getabsolutepath ();            if (!hprofpath.endswith ("/")) {hprofpath+= "/";            } hprofpath+= Createtime + ". Hprof";                try {android.os.Debug.dumpHprofData (Hprofpath);                Bool= true;            LOG.D ("Android_lab", "Create DumpFile done!"); }catCH (ioexception e) {e.printstacktrace ();            }} else {bool= false;        LOG.D ("Android_lab", "nosdcard!");    } return bool; }

Don't forget to declare sdcard Write permissions in Androidmanifest.xml:

<uses-permissionandroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>



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.