package com.jaeger.ninegridimgdemo.entity;
import android.content.Context;
import android.os.Debug;
import java.io.File;
import java.io.IOException;
/**
* Created by 杨阳洋 on 2017/11/23.
* usg:OOM异常
*/
public class OomExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final String FILENAME = "out-of-memory.hprof";
private final Thread.UncaughtExceptionHandler defaultHandler;
private final Context context;
public static void install(Context context){
Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
if(defaultUncaughtExceptionHandler instanceof OutOfMemoryError) {
return;
}
OomExceptionHandler oomHandler = new OomExceptionHandler(defaultUncaughtExceptionHandler, context);
Thread.setDefaultUncaughtExceptionHandler(oomHandler);
}
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 (IOException e) {
}
}
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;
}
}
内存泄漏Exception类-OomExceptionHandler
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 描述 在使用handler时,一般我们用于解决跨线程操作的问题,一般开启线程可能是处理耗时操作,因此很可能导致内存...
- Handler和内部类怎么引起内存泄漏? 先看下面一段代码: 我们平时使用Handler的时候,相信很多人都会这样...