Java - CMS vs G1 garbage collector
Asked Answered
R

1

6

What's the difference between cms and g1 garbage collector which makes g1 better?

Most of places it is said that this is because in G1, heap is divided into regions and then collection of regions are marked as young/old generation and gc runs on few regions and not on whole heap. I am trying to further understand this and have few more questions:

  1. When young gc runs (which is stop the world) it runs on all the young generation regions which implies whole of young part of heap and not few regions of young generation. Then is it identical to CMS in terms of time it takes ?

  2. Incremental compaction - Mixed collections - Now this is where i think G1 has advantage because concurrent marking of whole heap keeps on happening and there are mixed gc cycles which runs on all young regions + few old regions (with most garbage first). So it keeps on removing garbage from old generation as well instead of waiting for Full gc to happen. Is it correct ?

Are the above stated points correct ? And what are other differences which makes g1 better ?

Raincoat answered 2/1, 2021 at 6:46 Comment(5)
Which version of Java are you using? 11? 8? 14?Fichte
I am using java 11Raincoat
Then from Oracle's documentation: "The CMS collector is deprecated as of JDK 9." which would be the main difference. docs.oracle.com/en/java/javase/11/gctuning/…Fichte
My question is actually why G1 is better than CMS, i know CMS is not available in java 11 but i am trying to understand when G1 was introduced it was supposed to be better than CMS and why it is so ?Raincoat
Refer to this : #54616416Brazier
F
7

I will add a few reasons that I am aware of.

  • Pause Target Time

You can effectively instruct G1 to try at its best to do its work within that pause target time. Internally it will choose the number of regions to work on based on the statistics from previous collections. It will also resize regions because of that. You could not do that with CMS.

  • Remembered Sets

CMS only has a card table internal structure, which means that it needs to always be scanned entirely. On the other hand G1 uses Remembered Sets, which are smaller in size and can tell (quickly) what other regions need to be scanned as part of the current one.

  • Mixed Collections

Yes, G1 can scan young plus a small portion of old - called mixed collections (you can configure the size via flags), but this means it is a lot faster then simply scanning just the old, entirely.

  • Fragmentation

Most probably the biggest trigger in starting the theoretical work in the early days around G1, at least my "older" team mates that worked a lot with CMS, say that this was one of the biggest pains. CMS does not do any compaction, at all. When objects can't be moved to old generation (because the "gaps" are too small in that generation) - everything stops and that space needs to be worked on. This turned to be very expensive and took a lot of time. On the other hand, each G1 cycle does compaction as it moves live objects and leaves regions empty behind.


There are probably many other reasons that I am not aware of, yet.

Florenceflorencia answered 2/1, 2021 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.