Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get OutOfMemoryError because garbage collection too slow?

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.

like image 682
Jacky Chen Avatar asked Jan 03 '12 21:01

Jacky Chen


People also ask

Does garbage collection affect performance?

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.

What causes long garbage collection time?

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.

Does garbage collection affect CPU?

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.

What is the effect of garbage collection on memory?

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.


1 Answers

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)

like image 69
fge Avatar answered Oct 20 '22 21:10

fge