Re: memory allocation
Mark Space wrote:
It would be
interesting if there were some way to test this.
Following up on this:
I tried the program below. First, I left the line with "piggy.add( b )"
uncommented and got an OutOfMemory error after 62 iterations. This
seems right -- the default memory limit for a JVM is 64 megabytes.
Then I commented out that line and let the program run. It went over
1500 iteration before I killed it. No problems allocating large objects
there.
Then I decided to stress out the GC even more. I changed BUFF_SIZE from
one megabyte to 32 megabytes (see the second, commented out line for
BUFF_SIZE). It should be impossible to have more than one of these
objects in memory at the same time. The GC must always run and delete
the previous object or it won't be able to allocate a new one. I ran
this version over 500 times (it was much slower) before I killed it.
Short answer: I don't think Mr. Nicholas supposition in that blog
applies to modern JVMs anymore.
package fubar;
import java.util.HashSet;
import java.util.Set;
public class MemoryTest {
private static final int BUFF_SIZE = 1 * 1024 * 1024;
// private static final int BUFF_SIZE = 32 * 1024 * 1024;
private static final Set piggy = new HashSet();
public static void main( String[] args )
{
// Force an out of memory error
for( int i = 1;; i++) {
byte[] b = new byte[BUFF_SIZE];
System.out.println( i+" buffers alloacted" );
// piggy.add( b );
}
}
}