1

I noticed, app users got error:

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

So, I had written pretty simple app:

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();
    }
}

Who throws OOM while parsing StackOverflowException I am not able to get stacktrace in log:

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)

Have any an idea how I can fix that OOM or how I could get stacktrace in prod?

neworld
  • 7,757
  • 3
  • 39
  • 61
  • what does you want to do with this class? :) – gio Jan 21 '16 at 09:24
  • I want to get StackOverflowException – neworld Jan 21 '16 at 10:44
  • How does forcing SO instead of OOM exception in a sample project help you? To do the same thing in your complex product you'd have to know where the OOM is being thrown and if you'd known you could fix it. I don't understand your question. – Eugen Pechanec Jan 21 '16 at 10:56
  • Few years ago I got SO instead of OOM and fixed infinite recursions. Perhaps is a way to limit recursion depth or something else? Otherwise, I don't know how I can hunt down such bugs in +130k lines legacy project – neworld Jan 21 '16 at 11:02

1 Answers1

1

Update:

ThreadGroup group = new ThreadGroup("threadGroup");
new Thread(group, new Runnable() {
    private void stackOverflow() {
        stackOverflow();
    }
    @Override
    public void run() {
        stackOverflow();
    }
}, "name", 20).start();
gio
  • 4,950
  • 3
  • 32
  • 46
  • Still OOM. I will try to explain again. I want to get `StackOverflowException` instead of `OutOfMemoryException`. Without stack trace, I can't find out where the app is crashing for users. – neworld Jan 21 '16 at 10:49
  • @neworld Looks like stack will try to expand itself indefinitely until memory runs out. Meaning you can't get an SO. And if you could it would not be reliable. – Eugen Pechanec Jan 21 '16 at 10:57
  • Few years ago I got StackOverflow exceptions. Maybe is possible to limit recursion depth or something? – neworld Jan 21 '16 at 11:00
  • @gio it works! Is possible to reduce stack size for UI thread? According to [Create the UI thread manually in Android in order to increase its small stack size](http://stackoverflow.com/questions/16913317/create-the-ui-thread-manually-in-android-in-order-to-increase-its-small-stack-si) is impossible. Maybe is another way to get StackOverflow exception from prod version? – neworld Jan 21 '16 at 12:38
  • @neworld unfortunately, I did not find any solution for that, try to use some crash handling system like Fabric (Crashlytics) to figure out your problem. Usually such kind of services provide dump state of program (include all threads), maybe you'll be able to find place of you crash – gio Jan 21 '16 at 15:49
  • I already use fabric, but fabric crashes as well because app consumed all memory in that moment. @gio could you complete answer about that you told in the comments? and I will accept it. – neworld Jan 22 '16 at 07:36