有没有办法让Android进程在OutOfMemoryError上产生堆转储?

emm*_*mby 18 android dump out-of-memory hprof

-XX:+HeapDumpOnOutOfMemoryError每当Java进程用完堆时,sun JVM都支持转储堆的选项.

在Android上是否有类似的选项可以在OutOfMemoryException上创建一个Android应用程序转储堆?手动使用DDMS时,可能很难尝试正确计时.

jfr*_*z42 27

扩展CommonsWare的答案:

我不知道这是否有效,但你可能会尝试添加一个顶级异常处理程序,并在那里要求堆转储,如果它是一个OutOfMemoryError.

我使用以下代码在我自己的Android应用程序中成功地遵循了他的建议:

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

创建转储后,您需要将其从手机复制到PC:单击手机上的"打开USB存储",找到该文件并将其复制到硬盘中.

然后,如果要使用Eclipse Memory Analyzer(MAT)来分析文件,则需要hprof-conv.exe dump.hprof dump-conv.hprof转换文件:( hprof-conv位于其下android-sdk/tools)

最后,dump-conv.hprof用MAT 打开文件


Com*_*are 10

我不知道这是否有效,但你可能会尝试添加一个顶级异常处理程序,并在那里要求堆转储,如果它是一个OutOfMemoryError.


小智 10

这是一个改进版本.除了原始实现之外,此实现还支持:

  • 在所有线程上捕获内存不足错误(不仅在主线程上)
  • 识别内存不足错误,即使它隐藏在不同的错误中也是如此.在某些情况下,Out of Memory错误被封装在运行时错误中.
  • 也调用原始的默认未捕获异常处理程序.
  • 仅适用于DEBUG版本.

用法:initializeonCreate方法中调用Application类中的静态方法.

package test;
import java.io.File;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;

import android.os.Environment;
import android.util.Log;

import com.example.test1.BuildConfig;

public class OutOfMemoryDumper implements Thread.UncaughtExceptionHandler {

    private static final String TAG = "OutOfMemoryDumper";
    private static final String FILE_PREFIX = "OOM-";
    private static final OutOfMemoryDumper instance = new OutOfMemoryDumper();

    private UncaughtExceptionHandler oldHandler;

    /**
     * Call this method to initialize the OutOfMemoryDumper when your
     * application is first launched.
     */
    public static void initialize() {

        // Only works in DEBUG builds
        if (BuildConfig.DEBUG) {
            instance.setup();
        }
    }

    /**
     * Keep the constructor private to ensure we only have one instance
     */
    private OutOfMemoryDumper() {
    }

    private void setup() {

        // Checking if the dumper isn't already the default handler
        if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof OutOfMemoryDumper)) {

            // Keep the old default handler as we are going to use it later
            oldHandler = Thread.getDefaultUncaughtExceptionHandler();

            // Redirect uncaught exceptions to this class
            Thread.setDefaultUncaughtExceptionHandler(this);
        }
        Log.v(TAG, "OutOfMemoryDumper is ready");
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {

        Log.e(TAG, "Uncaught exception: " + ex);
        Log.e(TAG, "Caused by: " + ex.getCause());

        // Checking if the exception or the original cause for the exception is
        // an out of memory error
        if (ex.getClass().equals(OutOfMemoryError.class)
                || (ex.getCause() != null && ex.getCause().getClass()
                        .equals(OutOfMemoryError.class))) {

            // Checking if the external storage is mounted and available
            if (isExternalStorageWritable()) {
                try {

                    // Building the path to the new file
                    File f = Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

                    long time = System.currentTimeMillis();

                    String dumpPath = f.getAbsolutePath() + "/" + FILE_PREFIX
                            + time + ".hprof";

                    Log.i(TAG, "Dumping hprof data to: " + dumpPath);

                    android.os.Debug.dumpHprofData(dumpPath);

                } catch (IOException ioException) {
                    Log.e(TAG,"Failed to dump hprof data. " + ioException.toString());
                    ioException.printStackTrace();
                }
            }
        }

        // Invoking the original default exception handler (if exists)
        if (oldHandler != null) {
            Log.v(TAG, "Invoking the original uncaught exception handler");
            oldHandler.uncaughtException(thread, ex);
        }
    }

    /**
     * Checks if external storage is available for read and write
     * 
     * @return true if the external storage is available
     */
    private boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        Log.w(TAG,"The external storage isn't available. hprof data won't be dumped! (state=" + state + ")");
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)