comments

Share the article

Collaborative translation

Programming language Skills

Java garbage collection mechanismThe translated 100%

JavaSon712
Comment on the 17th
Java Garbage Collection Basics

17

Translation (4 persons) :Rhys_Lee.AzureSora.The crook section 9.Little rookie chicken

print
Only Chinese
Chinese and English
Only English

What is automatic garbage collection?

Automatic garbage collection is the process of monitoring heap memory, identifying used or unused objects, and then removing those unused objects. An object in use, or a referenced object, is a pointer to that object that is still maintained somewhere in the program. Unused objects, or unreferenced objects, are no longer referenced anywhere in the program. Therefore, memory occupied by unreferenced objects can be reclaimed.

In a programming language like C, allocating and reclaiming memory requires manual handling. In Java, the process of memory reclamation is handled automatically by the garbage collector. The basic process can be described as follows.


Rhys_Lee

Other Translations (1)

Step 1: Mark

The first step in this process is called tagging. At this stage, the garbage collector identifies which memory is being used and which memory is not.

Referenced objects are shown in blue. Unreferenced objects are shown in golden yellow. In the marking phase, all objects need to be scanned and judged. If all objects in the system had to be scanned, this would be a very time consuming process.


Rhys_Lee

Other Translations (1)

Step 2: Delete normally

In this phase, all unreferenced objects are removed, referenced objects are retained, and Pointers to free memory are maintained.

A memory allocator maintains a reference to a free block of memory where the memory of a newly created object is allocated.

Step 2 (a) : Delete and tidy

To further improve performance, in addition to removing unreferenced objects, you can also compress the remaining referenced objects. Moving referenced objects together makes it easier and faster to allocate memory for new objects.


Rhys_Lee

Other Translations (1)

Why generational garbage collection?

As mentioned earlier, marking and collating all objects in the JVM is inefficient. As more and more objects are allocated, the list of objects grows, resulting in longer and longer garbage collection times. However, based on empirical analysis of applications, most objects are short-lived.

The following is an example of such data. The Y-axis represents the number of bytes allocated, and the X-axis represents the number of bytes allocated over time.

As you can see, fewer and fewer objects are still alive over time. In fact, most objects have a short lifetime, as the higher value on the left side of the figure shows.


Rhys_Lee

Other Translations (1)

The JVM generational

What you learn from object allocation behavior can be used to improve the performance of the JVM. Therefore, consider splitting the heap into smaller parts or generations. For example: young generation, old generation, lasting generation.

The young generation is where all new objects are allocated and aged. When the young generation fills, the Minor GC is triggered. Minor GC can be optimized if the object has a high mortality rate. All dead objects of the younger generation can be quickly reclaimed. Some surviving objects will age and eventually be moved to the old age.

Stop the World — All Minor GC triggers “Stop the World”. This means that all application threads stop until the operation completes. The Minor GC always triggers Stop the Word.


Rhys_Lee

Other Translations (1)

Old age is used to preserve objects that live for a long time. Typically, a threshold is set at which the young generation objects are moved to the old age. Eventually the old will be recycled too. This event becomes the Major GC.

The Major GC also triggers STW (Stop the World). In general, the Major GC is much slower because it involves all living objects. Therefore, for responsive applications, Major GC should be avoided as much as possible. Also note that the duration of the STW for the Major GC is affected by the type of tenured garbage collector.

The persistent generation contains metadata that the JVM uses to describe classes and methods in the application. Permanent generations are populated by the JVM at run time based on the classes used by the application. In addition, Java SE class libraries and methods are stored here.

If the JVM finds that some classes are no longer needed, and others may need space, those classes may be reclaimed.


Rhys_Lee

Generational garbage collection process

Now that you understand why the heap is divided into different generations, it’s time to look at how these Spaces interact. The following images describe the object allocation and aging process in the JVM.

First, any new objects are allocated to Eden space. Both survivor Spaces are empty.

When Eden space fills up, a slight garbage collection is triggered.

The referenced object is moved to the first survivor space. When the Eden space is cleared, unreferenced objects are deleted.


The crook section 9

Other Translations (2)

In the next Minor GC, the Eden section does the same. Deletes unreferenced objects and moves referenced objects to Survivor zones. Here, however, they are moved to the second Survivor zone (S1). In addition, objects in the first Survivor zone (S0) that survived the previous Minor GC are aged up and moved to S1. After all surviving objects are moved to S1, S0 and Eden will be cleared. Notice that there are objects of different ages in the Survivor zone.

In the next Minor GC, the same operation is repeated. This time, however, Survivor zones swap. The referenced object is moved to S0. Surviving objects increase in age. Eden and S1 are cleared.


Rhys_Lee

This slide shows promotion. After the smaller GC, aged objects are promoted from the younger generation to the older generation when they reach a certain age threshold (8 in this example).

As smaller GCS continue to occur, objects will continue to be generalized to older Spaces.

So it almost covers the whole process of the younger generation. Eventually, GC will be performed primarily on the older generation, cleaning up and eventually compressing the space.


    The crook section 9

    Other Translations (1)
    CC

    Cool, dropping ES


    The flowers doubled up with laughter


    Syion


    obaniu


    foodon
    To report

    sC_Cs
    To report

    Evil eight children
    To report

    sauce
    To report

    Cloud sleeve disorderly dance month
    To report
    i
    iclannad
    To report
    1
    2
    >