[Android] generate heap dump file (. hprof)
Android generates the heap dump file (. hprof)
A heap dump is a heap snapshot of the program. You can know which parts of the program are using most of the memory.
It is saved as a binary format called HPROF. For files generated by running the Android. OS. Debug. dumpHprofData (hprofPath) method on android, You need to convert the. hprof file from Dalvik format to J2SE HPROF format. Use the hprof-conv tool provided by AndroidSDK to perform the conversion.
hprof-conv dump.hprof converted-dump.hprof
This article is originally written by sodino and published on the blog:Http://blog.csdn.net/sodino, Reprinted, please indicate the source.
The related code can be downloaded from the group file in the QQ Group Code2Share (363267446.
Android code generates the 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 an SdCard exists and is available if (android. OS. environment. MEDIA_MOUNTED.equals (state) {Filefile = new File (Environment. 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 ;}
Do not forget to declare the SDCard write permission in AndroidManifest. xml: