Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4504228

OutOfMemoryError appears when writing in ObjectOutputStream

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • None
    • 1.3.1
    • core-libs



      Name: nt126004 Date: 09/18/2001


      java version "1.3.1_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01)
      Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)

      It seems that ObjectOutputStream has some kind of memory leak, which causes
      programs that uses it intensively to fail.
      Here is little example that provides more explanation:

      [javacode]
      import java.net.*;
      import java.io.*;

      public class OutOfMemoryIssue implements Runnable
      {
      Socket clientside;
      static final int m_Size = 300000;
      public static void main(String[] args) {
      try
      {
      ServerSocket server = new ServerSocket(17000);
      Thread connector = new Thread(new OutOfMemoryIssue());
      connector.start();
      Socket serverside = server.accept();
      // InputStream is = serverside.getInputStream();
      ObjectInputStream is = new ObjectInputStream
      (serverside.getInputStream());
      int i = 0;
      byte[] data = new byte[m_Size];
      try
      {
      while(true)
      {
      // is.read(data);
      data = (byte[])is.readObject();
      i++;
      }
      }
      catch (Exception ex)
      {
      System.out.println("exc:"+ex);
      System.out.println("exited at:"+i);
      }
      connector.join();
      is.close();
      serverside.close();
      server.close();
      }
      catch(Exception ex)
      {
      System.out.println(ex);
      ex.printStackTrace();
      }
      }

      public void run()
      {
      // only for connecting to the server
      // OutputStream os = null;
      ObjectOutputStream os = null;
      int i = 0;
      try
      {
      clientside = new Socket("localhost", 17000);
      // os = new clientside.getOutputStream();
      os = new ObjectOutputStream(clientside.getOutputStream
      ());

      Thread.sleep(1000);
      while(true)
      {
      // os.write(new byte[m_Size]);
      os.writeObject(new byte[m_Size]);
      i++;
      }
      }
      catch(Exception ex)
      {
      System.out.println("exc:"+ex);
      }
      catch(OutOfMemoryError er)
      {
      System.out.println("exc:"+er);
      }
      System.out.println("run() exited at:"+i);

      try
      {
      os.close();
      clientside.close();
      }
      catch (IOException ex)
      {
      }
      }
      }
      [/javacode]

      it creates a socket pair and a thread, that writes till some exception
      (OutOfMemoryError) happened.

      So here is output it produces on my computer:
      [javacode]
      exc:java.lang.OutOfMemoryError
      run() exited at:110
      exc:java.io.EOFException: Expecting code
      exited at:110
      [/javacode]
      It means that while loop executed 110 times and … BUM :)
        Interesting that if I use standard write/read (even of
      ObjectOutputStream/ObjectInputStream) pair instead of writeObject/readObject,
      nothing terrible happens (no memory leaks etc).

      Please let me know if I do something wrong.

      Thank you in advance,
      Alexey.
      (Review ID: 132121)
      ======================================================================

            mwarressunw Michael Warres (Inactive)
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: