WriteObject will OutOfMemory!

hi,

I don't understand gc now! This simple example will throw OutOfMemory, Why? Does WriteObject have bug ?

public void test_ObjectWrite() throws Exception {

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("work/test.oos"));

int[] data;

for(int i =0;i<10000*10000*1000;i++) {

data = new int[1024*1024];

//for(int j=0;j<data.length;j++)

//oos.writeInt(data[j]);

oos.writeObject(data);

//oos.writeObject(new Date());

data = null;

}

oos.flush();

oos.close();

}

>

[603 byte] By [huanghwh] at [2007-9-26 1:22:11]
# 1

Using java -verbose:gc in this example the gc is not able to collect memory, so when it reaches its default maximum size it gives the OutOfMemoryError.

The offending object is oos because it seems to keep a reference to the objects that writes. So data is still reachable for the gc.

For testing that put the initialization of oss and the call to close whithin the loop.

The solution is calling oss.reset() in the loop. Now you will see the work of gc with -verbose:gc

Botella at 2007-6-29 0:58:52 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Botella, ok, I understood writeObject. oos keeps all references of the objects that I wrote into stream before close or reset. Thanks.--huanghwh
huanghwh at 2007-6-29 0:58:52 > top of Java-index,Java HotSpot Virtual Machine,Specifications...