How Serial Garbage Collector works in the HotSpot virtual machine

The Serial Garbage Collector was one of the first garbage collectors in the HotSpot VM. While this collector is running, the application is suspended and continues to run only after garbage collection has stopped.

Application memory is divided into three spaces:

  • Young generation. Objects are created in this particular area of memory.
  • Old generation. Objects that survive "minor garbage collection" are moved to this area of memory.
  • Permanent generation. Here metadata about objects, Class data sharing (CDS), String pool are stored. The permanent area is divided into two: read-only and read-write. Obviously, in this case, the read-only area is never cleaned up by the garbage collector.

The Young generation memory area consists of three areas: Eden and two smaller Survivor spaces - To space and From space. Most objects are created in the Eden area, with the exception of very large objects that cannot be placed in it and therefore are immediately placed in the Old generation. Survivor spaces are moved objects that have survived at least one garbage collection but have not yet reached the tenuring threshold to be moved to the Old generation.

When the Young generation fills up, the minor collection process starts in this area, as opposed to the full collection process. It happens as follows: at the beginning of work, one of the Survivor spaces - To space, is empty, and the other - From space, contains objects that survived previous builds. The garbage collector looks for live objects in Eden and copies them to To space, and then copies live "young" (that is, not survived the specified number of garbage collections) objects from From space there. Old objects from From space are moved to Old generation. After an easy assembly, From space and To space change roles, the Eden area becomes empty, and the number of objects in the Old generation increases.

If the To space overflows during the copying of live objects, then the remaining live objects from Eden and From space, which did not have enough space in To space, will be moved to the Old generation, regardless of how many garbage collections they survived.

Since when using this algorithm, the garbage collector simply copies all living objects from one area of memory to another, then such a garbage collector is called copying. Obviously, for the copy garbage collector to work, the application must always have a free memory area into which live objects will be copied, and this algorithm can be used for memory areas that are relatively small in relation to the total memory size of the application. Young generation just satisfies this condition (by default, on client machines, this area occupies about 10% of the heap (the value may vary depending on the platform)).

However, a different algorithm is used for garbage collection in Old generation, which takes up most of the entire memory.

In the Old generation, garbage collection takes place using the mark-sweep-compact algorithm, which consists of three phases. In the Mark phase, the garbage collector marks all live objects, then, in the Sweep phase, all unmarked objects are removed, and in the Compact phase, all living objects are moved to the beginning of Old generation, as a result of which free memory after cleaning is a continuous area. The compacting phase is performed in order to avoid fragmentation and simplify the memory allocation process in the Old generation.

When free memory is a contiguous area, then to allocate memory for the created object, you can use the very fast (about a dozen machine instructions) algorithm bump-the-pointer: the address of the beginning of free memory is stored in a special pointer, and when a request is received to create a new object, the code checks that there is enough room for the new object, and if so, it simply increments the pointer by the size of the object.

A sequential garbage collector is great for most applications that use up to 200 megabytes of heap, run on client-side machines, and do not have strict requirements for the amount of pauses that garbage collection takes. At the same time, the "stop-the-world" model can cause long pauses in the application when using large amounts of memory. In addition, the sequential algorithm does not optimally use the computing resources of the computer, and the sequential garbage collector can become a bottleneck when running an application on multiprocessor machines.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed