How does the G1 (Garbage First Collector) work?


In the JEP-248 discussion of the G1 definition ( Garbage First Collector as the default Garbage Collector on Java 9 . In this period, I've been hearing a lot of G1 quotes, but very little material has deeper details or comparisons, for example, with #, which would be the "standard" GC in production applications with minimum performance requirements.

/ p>

Since automatic memory management provided by the JVM is one of the crucial points of the platform, I would like a deeper insight into why (the pros and cons) of adopting this new garbage collector, a parallel with existing implementations.

asked by anonymous 25.07.2015 / 05:00

2 answers


Classic garbage collectors work more or less as follows:

  • They stop application execution;
  • They scan entire application memory to identify which objects can no longer be accessed, and free them from memory;
  • They summarize the application's execution;
  • This hang-up is a problem for large applications that need to be highly responsive, such as Facebook for example, because the more memory the application uses, the longer the downtime and the less responsiveness of the application.

    The current JVM collector, the Concurrent Mark and Sweep (CMS), performs part of the scan and the memory release concurrently with running the application (hence the name), to try to reduce downtime. This reduces the problem but does not resolve it.

    The Garbage First Collector (G1C) solves this problem using some techniques:

    • It scans memory without stopping application execution.
    • It divides the memory into blocks to allow for partial collections.
    • It allows setting a timeout for garbage collection.
    • It estimates how many memory blocks it can collect, within the time limit, using data from previous collections.
    • It prioritizes the collection of the blocks with more garbage.
    • He collects using evacuation, that is: he takes a block, moves what is not junk to another block and releases the first whole block.
    31.08.2015 / 18:33

    The best explanation about G1 in relation to CMS: Getting Started with the G1 Garbage Collector

    The text is a bit long (I do not intend to do the whole translation), but in summary:

    • Types of heap: permanent generation, old generation and new generation

    • G1 / li>

      G1 G1 Old Generation Collection / li>

    06.09.2015 / 21:01