RamPrakash
RamPrakash

Reputation: 3234

GC gets triggered often

I would like to understand why the GC gets triggered even though I have plenty of heap left unused.. I have allocated 1.7 GB of RAM. I still see 10% of GC CPU usage often.

I use this - -XX:+UseG1GC with Java 17

enter image description here

Upvotes: -1

Views: 342

Answers (1)

usuario
usuario

Reputation: 2467

JVMs will always have some gc threads running (unless you use Epsilon GC which perform no gc, I do not recommend using this unless you know why you need it), because the JVM manages memory for you.

Heap in G1 is divided two spaces: young and old. All objects are created in young space. When the young space fills (it always do eventually, unless you are developing zero garbage), it will trigger some gc cleaning unreferenced objects from the young and promoting some objects which are still referenced to old.

Those spikes in the right screenshot will correspond to young collection events (where unreferenced objects get cleaned). Young space is always much more small than the old space. So it fills frequently. That is why you see those spikes regarding there is much more memory free.

Disclaimer: This is a really very high level explanation of memory management in the JVM. Some important concepts have been not mentioned.

You can read more about G1GC collector here

Also take a look at jstat tool which will help you understand what is happening in your heap.

Upvotes: 1

Related Questions