Skip to main content

JVM garbage collection

2 replies [Last post]
Joined: 2007-07-12


I have a question about JVM garbage collection. I know that everytime i use the "new" operator it allocates the memory, so if i do it like this:

width = 1000;
height = 1000;
for(int i = 0; i < 1000; i++)
BufferedImage bi = new BufferedImage(1000,1000,BufferedImage.TYPE_INT_ARGB );

Will i get a out of memory problem? i mean will JVM allocates 1000 different memory location for "bi". or will JVM discard some of the "bi" that got created earlier when it is about to run out of memory.

Also, where is the JVM forum? i hope i put this message in the right place.



Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-01-07


As soon as not enough free memory is left, the JVM will start a garbage collection, so your code won't lead to any memory leak.
The reason is that old BI's are not reachable anymore (you can't access the BI you've generated in a previous iteration anymore), so the GC simply removes them.

However large BIs are usually large, so you won't get best performance with that code of course ;)

lg Clemens

Joined: 2007-07-12

thank you so much for reply. It is very helpful. My actual image will be much smaller than this it is like 320 by 240 and i have 4gb of ram. by the way, what is the maximum heap and stack size java can handle. I use Eclipse IDE with windows xp. here is the Eclipse.ini file:


can i set it like this:



i heard java max ram is at 1gb. is that true? If i allocate to the amount that is larger than java can handle, will i get a performance lost?

thank you

Francis Chan