有没有办法有一个Android的过程中产生的堆转储对一个OutOfMemoryError? [英] Is there a way to have an Android process produce a heap dump on an OutOfMemoryError?

查看:179
本文介绍了有没有办法有一个Android的过程中产生的堆转储对一个OutOfMemoryError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sun JVM的支持 -XX:+ HeapDumpOnOutOfMemoryError 选项转储堆每当一个java进程耗尽堆

The sun JVM supports a -XX:+HeapDumpOnOutOfMemoryError option to dump heap whenever a java process runs out of heap.

有没有在Android上的类似选项,这将使Android应用程序转储堆在一个OutOfMemoryException?它可以是很难尝试手动使用DDMS的时候适当地计时。

Is there a similar option on Android that will make an android app dump heap on an OutOfMemoryException? It can be difficult to try to time it properly when using DDMS manually.

推荐答案

要扩大在CommonsWare的回答是:

To expand upon CommonsWare's answer:

我不知道这是否正常工作,但你可以尝试<一href="http://developer.android.com/reference/java/lang/Thread.html#setDefaultUncaughtExceptionHandler%28java.lang.Thread.UncaughtExceptionHandler%29">adding顶层异常处理程序和在那里<一href="http://developer.android.com/reference/android/os/Debug.html#dumpHprofData%28java.lang.String%29">asking对于堆转储如果它是一个的OutOfMemoryError

I have no idea if this works, but you might try adding a top-level exception handler, and in there asking for a heap dump if it is an OutOfMemoryError.

我跟着他的建议成功地在自己与下面的code Android应用:

I followed his suggestion successfully in my own Android app with the following code:

public class MyActivity extends Activity {
    public static class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            Log.e("UncaughtException", "Got an uncaught exception: "+ex.toString());
            if(ex.getClass().equals(OutOfMemoryError.class))
            {
                try {
                    android.os.Debug.dumpHprofData("/sdcard/dump.hprof");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            ex.printStackTrace();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Thread.currentThread().setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
    }
}

创建转储

后,您需要将其从手机复制到PC:点击打开USB存储设备的电话,找到该文件,并将其复制到硬盘上

After the dump is created, you need to copy it from your phone to your PC: Click "Turn on USB storage" on the phone, find the file and copy it to your hard drive.

然后,如果你想使用Eclipse的内存分析器(MAT)分析文件,你需要隐蔽的文件: HPROF-conv.exe dump.hprof自卸conv.hprof (HPROF-CONV位于 Android的SDK /工具

Then, if you want to use the Eclipse Memory Analyzer (MAT) to analyze the file, you will need to covert the file: hprof-conv.exe dump.hprof dump-conv.hprof (hprof-conv is located under android-sdk/tools)

最后,打开自卸conv.hprof 与MAT文件

Finally, open the dump-conv.hprof file with MAT

这篇关于有没有办法有一个Android的过程中产生的堆转储对一个OutOfMemoryError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆