反序列化(readObject)一个LinkedList大约。 300个元素,每个元素包含另一个嵌套的ArrayList。每个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)
答案 0 :(得分:2)
根据提供的堆栈跟踪,您已嵌套ArrayList
十一级深度。
LinkedList
└ ArrayList
└ ArrayList
└ ArrayList
└ ArrayList
└ ArrayList
└ ArrayList
└ ArrayList
└ ArrayList
└ ArrayList
└ ArrayList
└ ArrayList
└ Date
那么,我该如何防止StackOverflowError?
为防止堆栈溢出,请将数据结构展平。
如果不可行,请查看How to increase the Java stack size?。