反序列化(readObject)时,具有大约300个元素,每个元素都包含另一个嵌套的ArrayList,其中包含大约3个元素。每个元素有10到30个元素,有时会出现StackOverflowError。 ObjectOutput / Input文件的大小为1MB。所有这些都是在Android上完成的。

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

这里是堆栈:

java.lang.StackOverflowError
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嵌套了11个层次。

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

那么,如何防止StackOverflowError?

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

或者,如果这不可行,请签出How to increase the Java stack size?

10-08 03:43