为什么解析StackOverflowException会抛出OOM?

时间:2016-01-21 09:00:24

标签: android recursion out-of-memory

我注意到,应用用户出错:

Fatal Exception: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available

所以,我写了非常简单的应用程序:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("TAG", "" + getFoo());
    }

    private int getFoo() {
        return getBar();
    }

    private int getBar() {
        return getFoo();
    }
}

在解析StackOverflowException时抛出OOM的人我无法在日志中获取堆栈跟踪:

01-20 13:51:06.250 8134-8134/lt.neworld.java E/art: Throwing OutOfMemoryError "Failed to allocate a 16482048 byte allocation with 12515386 free bytes and 11MB until OOM"
01-20 13:51:06.250 8134-8134/lt.neworld.java E/AndroidRuntime: Error reporting crash
     java.lang.OutOfMemoryError: Failed to allocate a 16482048 byte allocation with 12515386 free bytes and 11MB until OOM
         at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
         at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:125)
         at java.lang.StringBuffer.append(StringBuffer.java:278)
         at java.io.StringWriter.write(StringWriter.java:123)
         at com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:358)
         at com.android.internal.util.FastPrintWriter.appendLocked(FastPrintWriter.java:303)
         at com.android.internal.util.FastPrintWriter.write(FastPrintWriter.java:625)
         at com.android.internal.util.FastPrintWriter.append(FastPrintWriter.java:658)
         at java.io.PrintWriter.append(PrintWriter.java:691)
         at java.io.PrintWriter.append(PrintWriter.java:31)
         at java.lang.Throwable.printStackTrace(Throwable.java:324)
         at java.lang.Throwable.printStackTrace(Throwable.java:300)
         at android.util.Log.getStackTraceString(Log.java:340)
         at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:59)
         at com.android.internal.os.RuntimeInit.access$200(RuntimeInit.java:43)
         at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:85)
         at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
         at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

知道我如何修复OOM或者如何在prod中获得stacktrace?

1 个答案:

答案 0 :(得分:1)

更新

ThreadGroup group = new ThreadGroup("threadGroup");
new Thread(group, new Runnable() {
    private void stackOverflow() {
        stackOverflow();
    }
    @Override
    public void run() {
        stackOverflow();
    }
}, "name", 20).start();