Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle StackOverflowError happended when serialize a big object?
    primarykey
    data
    text
    <p>Here is my java code:</p> <pre><code> FileOutputStream fos = null; ObjectOutputStream out = null; try { fos = new FileOutputStream(pathName); out = new ObjectOutputStream(fos); out.writeObject(index); out.close(); fos.close(); } catch (IOException e) { LogManager.writeLogToFile(e.getMessage()); e.printStackTrace(); } </code></pre> <p>The index is an instance of inverted document index by myself. I have built the object index in memory, but when I pass it to the method writeObject(index), I get the error StackOverflowError. My question is: Why does this error happen after that I have built the object in the memory?</p> <p>Suppose that the data structure of index is as follow:</p> <pre><code>class InvertedIndex{ private HashMap&lt;String, PostList&gt; index; } class PostList{ private int size; private PostNode first; } class PostNode{ private String key; private double weight; private PostNode next; } </code></pre> <p>To avoid recursive call, I overwrite the method writeObject and readObject of the PostList because it may be a very long list:</p> <pre><code>class PostList{ private void writeObject(ObjectOutputStream out) throws IOException{ PostNode temp = first; while(temp != null){ out.writeObject(temp); temp = temp.getNext(); } } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{ in.defaultReadObject(); } } </code></pre> <p>Then I get two questions:</p> <ul> <li>out.writeObject(temp); Because temp has a reference to the next node, when I write temp, recursive call will also happen. The same situation will continue even if I overwrite the writeObject method of PostNode with defaultWriteObject. Am I right? If so, what can I do to avoid this recursive call?</li> <li>How to overwrite the method readObject()? </li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload