In java, is it possible that you get a OutOfMemoryError exception, even when there should have enough memory should garbage collection freed more memory? In other words, if there is not enough memory to allocate, will gc be forced to run before throwing the OutOfMemoryError?
Thanks.
The most common performance problem associated with Java™ relates to the garbage collection mechanism. If the size of the Java heap is too large, the heap must reside outside main memory. This causes increased paging activity, which affects Java performance.
If your application's object creation rate is very high, then to keep up with it, the garbage collection rate will also be very high. A high garbage collection rate will increase the GC pause time as well. Thus, optimizing the application to create fewer objects is THE EFFECTIVE strategy to reduce long GC pauses.
CPU usage will be high during a garbage collection. If a significant amount of process time is spent in a garbage collection, the number of collections is too frequent or the collection is lasting too long. An increased allocation rate of objects on the managed heap causes garbage collection to occur more frequently.
When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system.
There is one case where you might get an OOM which is neither heap related or address space related: it is when the JVM decides the GC takes too much time to run.
See here.
In other words, the answer to your original question, that is, "if there is not enough memory to allocate, will gc be forced to run before throwing the OutOfMemoryError", is yes.
(you will see from the link above that the ratio is 98%/2% for gc/running code, which is very high already)
(note 2: this is for Sun's/Oracle's JVM, no idea for other implementations)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With