United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4504228 : OutOfMemoryError appears when writing in ObjectOutputStream

Details
Type:
Bug
Submit Date:
2001-09-18
Status:
Closed
Updated Date:
2001-09-19
Project Name:
JDK
Resolved Date:
2001-09-19
Component:
core-libs
OS:
windows_2000
Sub-Component:
java.io:serialization
CPU:
x86
Priority:
P2
Resolution:
Duplicate
Affected Versions:
1.3.1
Fixed Versions:

Related Reports
Duplicate:

Sub Tasks

Description

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) 
======================================================================

                                    

Comments
WORK AROUND



Name: nt126004			Date: 09/18/2001


Customer Workaround:
if I use standard write/read (even of ObjectOutputStream/ObjectInputStream)
pair instead of writeObject/readObject, nothing terrible happens (no memory
leaks etc).
======================================================================
                                     
2004-06-11
EVALUATION

The described behavior is the expected behavior; see the Evaluation of 4363937 for more details.
###@###.### 2001-09-19
                                     
2001-09-19



Hardware and Software, Engineered to Work Together