Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to trace the source of a stack overflow error

I have a Java application that is working as a service. After several days running I get a stack overflow error. The problem is that the stack frame in my source where the error is being originated is not in the error reported by printStackTrace function. I have no idea about how to find where the problem is.

This is the result of printStackTrace:

java.lang.StackOverflowError
    at java.util.LinkedList.listIterator(LinkedList.java:684)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
        ... (this is repeated hundreds of times)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)
like image 370
David Avatar asked Jan 21 '26 11:01

David


2 Answers

Your problem can be reproduced as follows:

List<String> l = ...;

for (int i = 0; i < 2800; i++) {
    l = l.subList(...);
}
l.listIterator(...);

So, pay attention on all invocations of subList() and make sure they don't form a long chain of sublists as above.

Such a chain is a recursive structure (each sublist keeps a reference to its parent list), and calling listIterator() on a very long chain causes a very deep recursion which causes stack overflow.

like image 187
axtavt Avatar answered Jan 22 '26 23:01

axtavt


Look for uses of the List.subList() method - that's the only place where you can get an instance of java.util.SubList (a class with package visibility).

You could also try upading to a newer Java version - your stack trace doesn't match the structure of the list classes in current versions, so your problem may not occur (or result in a differen error that's easier to diagnose) on a more current version.

like image 34
Michael Borgwardt Avatar answered Jan 22 '26 23:01

Michael Borgwardt