JavaOne: G1 Garbage Collector


The Sun Hotspot guys have been working on a new garbage collector to replace CMS called G1. This presentation went over the differences between the old CMS and the new G1 collectors and also included some perspective from a guy at the Chicago Board of Options Exchange who has been beta testing it.

CMS divides the world into the young and old generations. This is done to take advantage of the observation that the lifetime of objects is highly uneven – the vast majority of objects die young glorious deaths and a very small number of objects live for a very long time (effectively the life of the app). Also important is that there tend to be very few references from the old generation to the young generation. Because of this, it’s ok to focus our collection attention on the young gen.

In CMS, new objects are created in the young generation which is further broken up into eden and two survivor spaces. Young gen GC checks to find live objects and those are put either in a survivor space or in the old generation, depending on age. Old gen gc is mostly concurrent but does stop-the-world pauses to finish up. Also stop-the-world for reference marking. Old gen gc is fragmented and sweep finds holes and manages in free lists. There is a fallback to full stop-the-world collection and compaction.

G1 (“garbage first”) takes a different approach – all memory (except perm gen) is broken into 1 MB “regions”. Young and old are both comprised of some set of non-contiguous regions but these change over time. During young gc survivors of a region are either copied to a new young gen region or to an old gen region as appropriate.

In G1, the old generation GC there is one stop-the-world pause to mark. If any region is found to contain no live objects, the region is immediately reclaimed (this happens more frequently than you’d expect due to locality). Then compact old regions into new old region. Old gen collections are piggybacked on young gen collections.

The technique for how G1 manages references into a region is called “remembered sets”. Every region has a small data structure (<5% of total heap) that reduces work needed to do marking. The remembered sets contain all external references into that region (references within the region are not included). After this initial layour by Tony Printezis (who was entertaining and explained things well), Paul Ciciora talked about how they test things at CBOE. Probably most important Paul said it is still a work in progress and not production-ready yet. One interesting item from the Q&A was that this will definitely be in Java SE 7 (probably committed in next few weeks) and that it will also be released in Java 6 update as well.


20 Responses to “JavaOne: G1 Garbage Collector”
  1. PAt says:

    Thanks for all these updates from JavaOne!

  2. hung says:

    I can’t stand the suspend… please tell me

    “Also important is that there tend to be very references from the old generation to the young generation.”

  3. radu says:

    @hung: few

  4. Alex says:

    @hung, @radu – sorry about that, fixed. Too much fast typing this week!

  5. Ole Friis Østergaard says:

    Thanks for the summary – very interesting. However, what’s the difference between the G1 approach and train garbage collection? It sounds very similar: Small “train wagons” that are in turn collected and reused, and “remember sets” that keep track of what other objects point to the current wagon. Train GC had really small individual garbage collection time slots, and thus was great for real-time systems (in principle), but was removed from Java because it simply performed too bad overall, as far as I know.

  6. Thanks Alex for the heads up information. Useful for analysis of the new JDK and help easier decision making whether to adapt the new version or not.

  7. Ravi Gupta says:

    Sounds very interesting. Would be nice if we can see some benchmarking details also.

  8. Mark says:

    Well I can say some good things about G1GC, we have some simulations that usually take an hour to maybe six hours on the long end. Our code is VERY memory intensive, we went to 64bit JVMs just to free up some memory limitations. We turned on G1GC via the -XX option and got a 25% reduction of runtime! This isn’t on JDK7 but on JDK1.6.0.14 on Centos 5.3 64bit. I can’t wait until JDK7 is out and we move to that but for now we’ll be turning on this option.

  9. Paul says:

    We also experienced a truly massive performance boost with G1 — we run on a 16 core machine with 64->128 GB RAM. Unfortunately, it crashes for us in production when running under high load (on JDK 1.6 _18) so we’ll have to wait and try again on the next build. :(

  10. Debmalya Jash says:

    How to turn on G1GC option?

  11. Alex says:

    @Debmalya: I think at the moment in JDK 1.6 you do
    -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC

    Presumably that will change to something less experimental before release.

  12. Roel Spilker says:

    It’s been a while since the announcements, and the java7 schedule is slipping. The G1is available in java6, but still experimental. Do you have any information if and when the G1 will officially be available in java6?

  13. Alex says:

    @Roel: no clue

  14. Durga Reddy says:

    It sound good :)

  15. Durga Reddy says:

    It sounds good :)

  16. Daniel says:

    G1 works on the same purpose and the same intention as CMS did. But it still works a quiet different. It’s a little complicated and some information about that vary from source to source. The exact routine is described here:

    I have to mention, that G1 can be slower than CMS – in some of my measurings G1 took 10-20% more time in JDK 1.6 u22 on Windows and Mac.


Check out what others are saying about this post...
  1. […] de libération mémoire des objets du développeur. Le nouveau Garbage Collector “Garbage First G1” devrait passer moins de temps en pause et ainsi d’améliorer l’efficacité de la […]

  2. […] O coletor de lixo G1 (G1 Garbage Collector) – Alex Miller […]

  3. […] официално ще има поддръжка на новия garbage collector G1. Въпросният съществува от месеци с възможност за […]