代码中dumphprof数据

来源:互联网 发布:医院排号系统编程 编辑:程序博客网 时间:2024/06/06 03:48

转载自:https://gist.github.com/pyricau/4726389fd64f3b7c6f32

    Dump the heap on OutOfMemoryError crashes in your debug builds.  

Raw        
                                    OomExceptionHandler.java                               
 import android.content.Contextimport android.os.Debugimport java.io.File;   public class OomExceptionHandler implements Thread.UncaughtExceptionHandler {     private static final String FILENAME= "out-of-memory.hprof";     public static void install(Contextcontext) {     Thread.UncaughtExceptionHandler defaultHandler= Thread.getDefaultUncaughtExceptionHandler();     if (defaultHandler instanceofOomExceptionHandler) {       return;     }     OomExceptionHandler oomHandler = new OomExceptionHandler(defaultHandler, context);     Thread.setDefaultUncaughtExceptionHandler(oomHandler);   }     private final Thread.UncaughtExceptionHandler defaultHandler;   private final Context context;     public OomExceptionHandler(Thread.UncaughtExceptionHandlerdefaultHandler, Context context) {     this.defaultHandler = defaultHandler;     this.context = context.getApplicationContext();   }     @Override public void uncaughtException(Threadthread, 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;   } }