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.