heap dump是一个程序heap的快照,可以获知程序那些部分正在使用大部分的内存.
对于Android执行android.os.Debug.dumpHprofData(hprofPath)生成程序的dump
fun createDumpFile(): Boolean {
val LOG_PATH = "/dump.gc/"
var bool = false
val sdf = SimpleDateFormat("yyyy-MM-dd_HH.mm.ssss", Locale.getDefault())
val createTime = sdf.format(Date(System.currentTimeMillis()))
val state = android.os.Environment.getExternalStorageState()
// 判断SdCard是否存在并且是可用的
if (android.os.Environment.MEDIA_MOUNTED == state) {
val file = File(Environment.getExternalStorageDirectory().getPath() + LOG_PATH)
if (!file.exists()) {
file.mkdirs()
}
var hprofPath = file.absolutePath
if (!hprofPath.endsWith("/")) {
hprofPath += "/"
}
hprofPath += "$createTime.hprof"
try {
android.os.Debug.dumpHprofData(hprofPath)
bool = true
XLog.e("create dumpfile done!--path:$hprofPath")
} catch (e: IOException) {
e.printStackTrace()
}
} else {
bool = false
XLog.e("no sdcard!")
}
return bool
}
网友评论