Focus on “Java Backend Technology Full Stack”

Respond to “000” to get lots of e-books

Heaps have already been mentioned in the previous article, just to give you an overview. This article takes a closer look at heaps in the JVM.

In the JVM, the heap is divided into two distinct regions: Young and Old.

Young is divided into three regions: Eden, From Survivor, and To Survivor.

The purpose of this division is to enable the JVM to better manage objects in heap memory, including memory allocation and collection.

Object and array creation allocates memory space in the Heap. The key is that there are so many areas in the Heap. What area is the object being created in?

The region where the object was created

In general, all newly created objects are allocated to the Eden section, and some particularly large objects are allocated directly to the Old section.

For example, objects A, B, C, etc. are created in the Eden area, but the memory space of the Eden area is certainly limited, such as 100M. If 100M has been used or A set critical value has been reached, then the Eden memory space needs to be cleaned, that is, Garbage collection. Such a collection is called a Minor GC, and a Minor GC is the Young GC.

After GC, some objects will be cleaned up, and some objects may still be alive. The surviving objects need to be copied to the Survivor area, and then cleared from the Eden area.

The full name of TLAB is Thread Local Allocation Buffer. By default, each Thread is allocated a Buffer region to speed up object Allocation. The buffer is in the Eden section.

This works similarly to ThreadLocal in the Java language, avoiding operations on public areas and some lock contention.

Object allocation takes precedence over TLAB, but TLAB is usually small, so when objects are relatively large, they are allocated in the shared area of the Eden section.

TLAB is an optimization technique, similar to object allocation on the stack (which can lead to the topic of escape analysis, turned on by default). This is a very detailed optimization, not too much introduction, but occasionally interview will be asked.

Survivor area,

As you can see From the diagram, Survivor is divided into two S0’s and S1, which can also be called From and To. At the same point in time, only one extent in S0 and S1 can have data, and the other one is empty.

For example, at first only the Eden section and the From section have objects, and the To section is empty.

At this point, a GC operation is performed, and the age of objects in the From section is +1. We know that all surviving objects in the Eden section will be copied To the To section, and surviving objects in the From section will have two places To go.

‐XX:+MaxTenuringThreshold) if the age of the object has reached the previously set age threshold (default age is 15 years, you can set your own parameter ‐XX:+MaxTenuringThreshold), then the object will be moved To the Old section, and if the Eden and From sections have not reached the threshold the object will be copied To the To section.

At this point, the Eden and From sections have been emptied (objects that were collected are definitely gone, and objects that were not collected have gone to their own places).

At this point, From and To switch roles, and the previous From becomes To, and the previous To becomes From. That is, make sure that the Survivor region named To is empty no matter what.

The Minor collection repeats this process until the To field is filled, and then copies all objects into the old age.

Old area overview

Can be seen from the above analysis, the general Old area are age larger objects, or relatively more than a certain threshold (- XX: PretenureSizeThreshold, the default is 0, said all in Eden area) of the object. There will also be a GC operation in the Old section, which is called a Major GC.

The entire life cycle of the object

Object 1

I am a normal Java object. I was born in Eden. In Eden, I saw my little brother who looked very similar to me.

One day the Eden section was so crowded that I was forced To go To the “From” section of the Survivor section. Ever since I went To the “From” section of the Survivor section, I’ve been floating around, sometimes on the “From” section of the Survivor section, sometimes on the “To” section of the Survivor section.

Until I was 18 years old, my father said I was an adult, it was time to go into the society.

So I went to the old generation. There were a lot of people in the old generation, and they were all quite old. I met a lot of people here. In the old generation, I live for 20 years (plus one year for each GC) and then get recycled.

Object 2

I was born a special case, a different person. I was born as big as an adult, so Eden said you were too big for us here, and sent me straight to the senior section. Mumming around in old age and dying of old age (recycled).

Common problems in the heap

How to understand Minor/Major/Full GC?

Please tell me where the Minor/Major/Full GC is sent.

Minor GC: A GC that occurs in a young generation


Major GC: GC that occurs in an older age.


Full GC: Cenozoic + Old Age, such as Metaspace area causes the collection of young and old ages.

Why do I need a Survivor zone? Can’t it just be Eden?

If there were no Survivor, the Eden region would be sent to the old age every time a Minor collection occurs and there would be no age limit.

In this way, the old age fills up quickly, triggering a Major GC(since a Major GC is usually accompanied by a Minor GC, it can also be considered triggering a Full GC).

The older generation had much more memory space than the new generation, and a Full GC took much longer than a Minor GC.

What’s the downside of a long implementation time?

Frequent Full GC can take a long time and affect the execution and responsiveness of large programs.

You might say, well, make more or less space for the old age.

If you increase the old age space, more live objects can fill the old age. Although the frequency of FullGC is reduced, with the increase of space in the older age, once fullGC occurs, the execution time is longer.

If you reduce the old age space, the Full GC takes less time, but the old age is quickly filled with living objects, and the frequency of Full GC increases.

So the purpose of a Survivor is to reduce the number of objects sent to the old age, thereby reducing the number of Full GC occurrences. The pre-filtering guarantee for a Survivor is that only objects that survive 16 Minor collections will be sent to the old age.

Why do I need two Survivor regions of the same size?

The biggest benefit is that fragmentation is resolved. So why wouldn’t a Survivor area work?

In Part 1, we learned that you had to set the Survivor zone. Assuming there is only one Survivor region, let’s simulate the flow:

The newly created object is in Eden. Once Eden is full, a Minor collection is triggered, and the surviving objects in Eden are moved to the Survivor area.

The next time Eden is full, there’s a Minor collection, and Eden and Survivor each have some survivors, and if you force the Survivor onto the Survivor, it’s clear that the memory held by the Survivor is not contigous. This leads to memory fragmentation.

There is always a Survivor space that is empty and a non-empty Survivor space that is free of fragments.

Why is Eden:S1:S2 8:1:1 in the new generation?

Available memory in the new generation: The replication algorithm guarantees 9:1 memory, so only 10% of the space is wasted. In the available memory, the Eden:S1 block is 8:1, that is, in the new generation, the Eden:S1:S2 = 8:1:1

This ratio is configured with the -XX:SurvivorRatio parameter (default is 8).

Recommended reading:

Inside MySQL Technology (5th Edition). PDF

Forty-five Habits of Highly Effective Programmers: The Instruction of Agile Development. PDF

Docker’s Complete Guide. PDF

Pay attention to the public number “Java back-end technology full stack”

Free access to 500GB of the latest learning materials