java 在调试版本中将堆转储到OutOfMemoryError崩溃上。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 在调试版本中将堆转储到OutOfMemoryError崩溃上。相关的知识,希望对你有一定的参考价值。
import android.content.Context;
import android.os.Debug;
import java.io.File;
public class OomExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final String FILENAME = "out-of-memory.hprof";
public static void install(Context context) {
Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
if (defaultHandler instanceof OomExceptionHandler) {
return;
}
OomExceptionHandler oomHandler = new OomExceptionHandler(defaultHandler, context);
Thread.setDefaultUncaughtExceptionHandler(oomHandler);
}
private final Thread.UncaughtExceptionHandler defaultHandler;
private final Context context;
public OomExceptionHandler(Thread.UncaughtExceptionHandler defaultHandler, Context context) {
this.defaultHandler = defaultHandler;
this.context = context.getApplicationContext();
}
@Override public void uncaughtException(Thread thread, Throwable ex) {
if (containsOom(ex)) {
File heapDumpFile = new File(context.getFilesDir(), FILENAME);
try {
Debug.dumpHprofData(heapDumpFile.getAbsolutePath());
} catch (Throwable ignored) {
}
}
defaultHandler.uncaughtException(thread, ex);
}
private boolean containsOom(Throwable ex) {
if (ex instanceof OutOfMemoryError) {
return true;
}
while ((ex = ex.getCause()) != null) {
if (ex instanceof OutOfMemoryError) {
return true;
}
}
return false;
}
}
以上是关于java 在调试版本中将堆转储到OutOfMemoryError崩溃上。的主要内容,如果未能解决你的问题,请参考以下文章
了解 Java 堆转储
Java webstart 堆转储
解析堆转储文件
Java堆转储在Google App引擎标准环境中?
在保留一些堆信息的同时减少托管程序的小型转储的大小?
用于分析大型 Java 堆转储的工具