3 Replies Latest reply: Aug 12, 2010 5:13 AM by 843790 RSS

    Outofmemory Error in ObjectInputStream

    843790
      Hi all

      In our application we are Serializing the Object from one process (JVM) and deserializing the same Object from another process(JVM). Both serialization and deserialization will happen concurrently from Different JVM.

      We are often getting OOM while de-serialization of the Object. We are using jdk 1.4 for compilation and Jre 1.6 for Running the JVM.

      We have allocated 512Mb for the Process which is doing the deserialization. We have analyzed the hprof file is just 50Mb, JVM is not dumping the 512Mb.

      We have googled about this OOM and suspecting that ObjectinputStream is leaking the Memory

      is it a defect in ObjectInputStream ?

      Could any one help us on this ?

      The stack trace is
      ----------------------------

      Exception in thread "Thread-9" java.lang.OutOfMemoryError: Java heap space
      at java.lang.reflect.Array.newArray(Native Method)
      at java.lang.reflect.Array.newInstance(Array.java:52)
      at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1631)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323)
      at java.io.ObjectInputStream.access$300(ObjectInputStream.java:188)
      at java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2109)
      at java.io.ObjectInputStream.readFields(ObjectInputStream.java:519)
      at java.lang.StringBuffer.readObject(StringBuffer.java:625)
      at sun.reflect.GeneratedMethodAccessor81.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
      at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1667)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323)
      at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1947)
      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1871)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
      at java.util.Hashtable.readObject(Hashtable.java:859)
      at sun.reflect.GeneratedMethodAccessor82.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1849)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)



      Thanks
      Velmurugan
        • 1. Re: Outofmemory Error in ObjectInputStream
          EJP
          You need to call ObjectOutputStream.reset() occasionally. It remembers objects it has already serialized so it can send 'handles' instead of resending the objects, so that arbitrary object graphs can be serialized. So find a point at which you stop caring about that and call reset(). Could be for every object if they are simple enough.
          • 2. Re: Outofmemory Error in ObjectInputStream
            843790
            Hi

            Thanks a lot for your reply,

            The serialization happens in different JVM with respect to de-serialization.

            Moreover, we are calling the reset of ObjectOutputStream while doing the Serialization.

            oos = new ObjectOutputStream(outFile);
            oos.writeObject(obj);
            oos.reset();
            oos.flush();

            How can we prevent the OOM at de-serialization at this JVM ? what could be the root cause for this OOM ?

            Thanks
            Velmurugan
            • 3. Re: Outofmemory Error in ObjectInputStream
              843790
              Hello

              just run in the same problem: deserialising causes OOM.

              The solution was to also call on the sending side the
              ObjectOutputStream .reset().

              You can easily check this overwriting the Object,'s ( which is serialized ) finalize method
                   // insert to check for no memory leak
                   // + call System.gc(); System.gc()
                   // @Override
                    protected void finalize() throws Throwable {
                    System.out.println("MulticastPayload finalize called");
                    super.finalize();
                    }
              and in the receivin thread call
              System.gc(); System.gc();

              %-)