I want to seriliaze an object with this method :
public void serializ(CRDT m) throws IOException {
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(byteOutput);
stream.writeObject(m);
sumMemory = byteOutput.size();
stream.flush();
stream.close();
byteOutput.flush();
byteOutput.close();
}
I have an exception java.lang.StackOverflowError
Exception in thread "main" java.lang.StackOverflowError
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1169)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
...
I read some forum that i need to reimplementing the writeObject / readObject
methods.
Is it the only possible solution ? and how to reimplementing the writeObject / readObject
My object to serialize is : http://pastebin.com/D1kEidtn
Two class that cause errors are : pastebin.com/Sb3X0Quq and enter link description here
CRDT is the superclass of the Object m which is serialized.
The error is that the class that derives from CRDT seems to have a reference to itself, leading to an endless recursion
You see that in the stack trace.
Tipp find out the class of The object CRDT m: by either using the debugger, or adding an System.out.println(m.getClass()) at the beginn of your serialize() method.
Then when you know the class, check wheter the object has a reference to itself.
You haven't posted your concrete classes so its hard to spot the error but
In short, any recursive algorithm can overflow the stack and stack is finite.
For deeply nested object graphs Java builtin serlialization requires excessive stack space.
for elaborate details refer
http://c2.com/cgi/wiki?JavaSerializationIsBroken
http://c2.com/cgi/wiki?JavaSerializationAndTheStack
i juste add -Xss512m
to Netbeans and it's work :D
BTW, I had same issue but looks like mine was related to checkpoint duration being too big. It appear, Spark maintains some sort of object linkage while writing it out and if the duration is too big it will get stack overflown.
来源:https://stackoverflow.com/questions/13541778/stackoverflowerror-when-i-serialize-an-object