[G1]Why run Full GC at System.gc() call?

stefan.johansson at oracle.com stefan.johansson at oracle.com
Mon Aug 24 07:22:56 UTC 2020


You are correct. When calling System.gc() with G1 a full GC is 
scheduled. You can change this by running with the option:

Doing so will instead initiate a concurrent cycle by running a young 
collection. There have been discussions if this should be the default 
behavior for G1, but there are arguments both ways. I think the current 
behavior is good, since it makes it really obvious this is an expensive 
operation that we do not really encourage people to use.

One of the few good uses of System.gc() is when you know your 
application doesn't suffer from doing garbage collections, for example, 
you know you have a lot of dead objects and currently no requests you 
have to handle. In this case a full collection is a better fit since it 
will reclaim as much memory as possible, while the concurrent cycle will 
only start reclaiming memory from the old generation in the mixed 
collections that will follow.

I hope this helps,

On 2020-08-23 19:54, Manavjeet Singh wrote:
> Hi!
> I was trying to understand what happens when System.gc() is called from a
> java application with G1 as the garbage collector. I understood
> G1CollectedHeap::collect() is called and then
> G1CollectedHeap::try_collect().
> But going through the code of G1CollectedHeap::try_collect() and using the
> "-verbose" flag while running the java application, I realised only a full
> GC will always be scheduled.
> Can you please help me understand this design decision or if I am wrong in
> understanding it.
> Thanks!
> Manavjeet Singh

More information about the hotspot-gc-dev mailing list