Java.util.Sublist throwing a StackOverFlowError

We get random StackOverFlowError errors in production related to performing a SubList operation. Has anyone seen something like this before and knows what could cause it?

This is the code that causes the call that causes the error:

  FacesContext context = FacesContext.getCurrentInstance();
    String newViewID = context.getViewRoot().getViewId();

    if (newViewID != null) {
     if (breadCrumbs.contains(newViewID)) {
      // Trims the list upon going back to allow for multiple back button requests.  
      // This is lightweight and not intended for a complex circular navigation.
      breadCrumbs = breadCrumbs.subList(0, breadCrumbs.indexOf(newViewID) + 1);
     } else {
      breadCrumbs.add(newViewID);
     }
    }

Result:

Caused By: java.lang.StackOverflowError
 at java.util.SubList$1.<init>(AbstractList.java:688)
 at java.util.SubList.listIterator(AbstractList.java:687)
 at java.util.SubList$1.<init>(AbstractList.java:688)
 at java.util.SubList.listIterator(AbstractList.java:687)
 ...
+5
source share
6 answers

The problem was caused by the fact that breadCrumbs is a LinkedList - we added too many elements to LinkedList and called subList to identify this problem.

0
source

The subList () method returns the view supported by the original list.

According to javadoc:

, undefined, (.. ) - . ( , .)

, - , , , , .

+6

LinkedList fastutil objectarraylist (fastutil - Java).

window = window.subList(index+1, window.size());

stackoverflow.

window = new LinkedList<>( window.subList(index+1, window.size()) );

.

,

+2

:

681    public ListIterator<E> listIterator(final int index) {
...
687        return new ListIterator<E>() {
688            private ListIterator<E> i = l.listIterator(index+offset);

StackOverflowError , l - current , , listIterator() .

breadCrumbs? getClass()?

0

, LinkedList. subList . , , subList, / . , , StackOverFlowError.

0

, AbstractList.java( ArrayList) subList. (aka view) , . subList , , , ( ..)

(, ) . , StackOverflowError.

:

public static void main(String[] args) {
    List<String> lst = new ArrayList<String>();
    lst.add(""); 
    for (int i = 0; i < 50000; i++) {
        lst.set(0, "test");
        lst = lst.subList(0, 1);
    }

    lst.add("test2");       
}

: SubList:

breadCrumbs = breadCrumbs.subList(0, breadCrumbs.indexOf(newViewID) + 1);

, .

: http://programmingtipsandtraps.blogspot.com/2013/05/javautillistsublist-stackoverflowerror.html

0
source

All Articles