Garbage Collection Before OutOfMemoryError

This is a garbage collection question in java: if you allocate a large block of memory (using the new int [BIG_NUMBER] or else), is there any guarantee that the garbage collector will collect garbage before throwing OutOfMemoryError? Is this garbage collector behavior part of the Java specification now?

I found out that the garbage collector can raise this exception if it takes too long to collect garbage, at least in the case of the Java Hotspot Java Virtual Machine

"A parallel / parallel collector will raise an OutOfMemoryError if too much time is spent collecting garbage: if more than 98% of the total time is spent collecting garbage and less than 2% of the heap is recovered, OutOfMemoryError will be thrown. This function is designed to prevent applications from running for a long period time, and almost nothing happens, because the heap is too small. If necessary, this function can be disabled by adding the -XX: -UseGCOverheadLimit parameter to the command line. "

but this can be disabled, according to the quotation, by adding an option to the line command.

+2
source share
2 answers

AFAIK, he was always part of Spec.

An early OutOfMemoryError, if the virtual machine is very small in memory, is a feature of Java 6 and was introduced to stop the virtual machine when it becomes unusable, but not completely dead.

I would not disable this feature, it is much better for you to develop your system so that you never come close to 98% memory usage. I would suggest that 30% is a more convenient level for work.

From Javadoc for Java 1.4.2 (introduced in 2002) http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/OutOfMemoryError.html

It is thrown when the Java virtual machine cannot allocate an object because it has no memory and no garbage collector can be provided anymore.

+3
source

It is simple, no. Your program may call Runtime.gc () for help.

-2
source

All Articles