StackOverflowError: LinkedList & ArrayList deserialization

StackOverflowError: LinkedList & ArrayList deserialization

当反序列化 (readObject) 一个 LinkedList 大约。 300 个元素,每个元素都包含另一个嵌套的 ArrayList,大约有每个 10-30 个元素,我有时会得到 WhosebugError。 ObjectOutput/Input 文件的大小为 1MB。所有这些都是在 Android.

上完成的

我已经在这里找到了一些类似的问题,但是 none 帮助我解决了这个问题。我该如何面对这个问题?我读过一个想法来遍历列表并将每个包含的对象本身写入 ObjectOutputStream 但后来我查看了 ArrayList 的 writeObject 方法并想通了,它已经以这种方式完成了。那么,如何防止 WhosebugError?

这里是堆栈:

java.lang.WhosebugError
at java.io.FilterInputStream.read(FilterInputStream.java:114)
at java.io.ObjectInputStream.checkReadPrimitiveTypes(ObjectInputStream.java:405)
at java.io.ObjectInputStream.read(ObjectInputStream.java:539)
at libcore.io.Streams.readFully(Streams.java:81)
at java.io.DataInputStream.readLong(DataInputStream.java:147)
at java.io.ObjectInputStream.readLong(ObjectInputStream.java:1400)
at java.util.Date.readObject(Date.java:799)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.ArrayList.readObject(ArrayList.java:661)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.LinkedList.readObject(LinkedList.java:984)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at java.util.HashMap.readObject(HashMap.java:1023)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
at org.felikz.app.WebserviceApplication.reloadAppState(Unknown Source)

根据提供的堆栈跟踪,您嵌套了 ArrayList 的十一层。

LinkedList
└ ArrayList
  └ ArrayList
    └ ArrayList
      └ ArrayList
        └ ArrayList
          └ ArrayList
            └ ArrayList
              └ ArrayList
                └ ArrayList
                  └ ArrayList
                    └ ArrayList
                      └ Date

So, how can I prevent the WhosebugError?

为防止堆栈溢出,请展平您的数据结构。

或者如果那不可行,请查看 How to increase the Java stack size?