Android生成heap dump文件(.hprof)
一个heap dump就是一个程序heap的快照,能够获知程序的哪些部分正在使用大部分的内存。
它保存为一种叫做HPROF的二进制格式。对于Android运行android.os.Debug.dumpHprofData(hprofPath)方法后所生成的文件,须要把.hprof文件从Dalvik格式转换成J2SE HPROF格式。使用AndroidSDK提供的hprof-conv工具可运行该转换操作。
hprof-conv dump.hprof converted-dump.hprof
本文属sodino原创,发表于博客:/sodino,转载请注明出处。
相关代码能够从QQ群Code2Share(363267446)中的群文件里下载。
Android代码生成dump文件例如以下:
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();// 推断SdCard是否存在而且是可用的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;}
不要忘记了在AndroidManifest.xml中声明SDCard写权限:
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />