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:
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 ?
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 ?
I will add a few reasons that I am aware of.
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
.
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.
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.
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.