This is a question about garbage collection in java: if one allocates a large block of memory (using new int[BIG_NUMBER] or however), is there any guarantee that a garbage collector will collect the garbage before an OutOfMemoryError is thrown? Is this behaviour
of garbage collector a part of the Java spec by now?
I have learnt that a garbage collector itself might throw such exception in case it takes too long to collect the garbage, at least in the case of Sun Hotspot Java Virtual Machine
«The parallel / concurrent collector will throw an OutOfMemoryError if too much time is being spent in garbage collection: if more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered, an OutOfMemoryError will be thrown. This feature is designed to prevent applications from running for an extended period of time while making little or no progress because the heap is too small. If necessary, this feature can be disabled by adding the option -XX:-UseGCOverheadLimit to the command line. »
but this may be disabled, according to the citation, by adding an option to the command
line.
AFAIK, It has always been part of the Spec.
The early OutOfMemoryError if the VM is very low on memory is a Java 6 feature and was introduced to stop the VM when it becomes unusable but not quite dead.
I wouldn’t turn this feature off, you are far better off engineering your system so you never get close to 98% memory usage. I would suggest 30% is a more comfortable level to run at.
From the Javadoc for Java 1.4.2 (introduced in 2002) http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/OutOfMemoryError.html