轉(zhuǎn)自:http://blog.csdn.net/sodino/article/details/38512473
一個heap dump就是一個程序heap的快照,可以獲知程序的哪些部分正在使用大部分的內(nèi)存稠肘。
它保存為一種叫做HPROF的二進制格式冀痕。對于Android執(zhí)行android.os.Debug.dumpHprofData(hprofPath)方法后所生成的文件荔睹,需要把.hprof文件從Dalvik格式轉(zhuǎn)換成J2SE HPROF格式。使用AndroidSDK提供的hprof-conv工具可執(zhí)行該轉(zhuǎn)換操作言蛇。
hprof-conv dump.hprof converted-dump.hprof
Android代碼生成dump文件如下:
public static boolean createDumpFile(Context context) {
String LOG_PATH = "/dump.gc/";
boolean bool = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ssss", Locale.getDefault());
String createTime = sdf.format(new Date(System.currentTimeMillis()));
String state = android.os.Environment.getExternalStorageState();
// 判斷SdCard是否存在并且是可用的
if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {
File file = new File(Environment.getExternalStorageDirectory().getPath() + LOG_PATH);
if (!file.exists()) {
file.mkdirs();
}
String hprofPath = file.getAbsolutePath();
if (!hprofPath.endsWith("/")) {
hprofPath += "/";
}
hprofPath += createTime + ".hprof";
try {
android.os.Debug.dumpHprofData(hprofPath);
bool = true;
Log.d("nsz", "create dumpfile done!");
} catch (IOException e) {
e.printStackTrace();
}
} else {
bool = false;
Log.d("nsz", "no sdcard!");
}
return bool;
}
不要忘記了在AndroidManifest.xml中聲明SDCard寫權(quán)限:
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />