David
David

Reputation: 3142

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)

Upvotes: 3

Views: 468

Answers (2)

axtavt
axtavt

Reputation: 242716

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.

Upvotes: 3

Michael Borgwardt
Michael Borgwardt

Reputation: 346377

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.

Upvotes: 1

Related Questions