Let’s go over the article before we read it.

What object can directly enter the old age?

You may say it quickly, after all, the eight-part essay is not written in vain.

  • Big object goes straight to the old age.

  • Dynamic age judgment

  • The age exceeds the threshold and enters the old age

  • After the Minor GC, the surviving object space is larger than the survivor space, and the old age is directly entered.

This time, we focus on dynamic age judgment. Here’s an image from the JVM book:

When can objects enter old time space?

One trigger: dynamic age judgment.

To put it simply: in survivor, all objects of the same age are larger than the normal space of survivor. Objects older than or equal to that age can enter the old age.

But is that really the case?

Prerequisite: Now I assume that the survivor space has 100M space.

To come. Let me give you an example. Just follow my lead.

Suppose an object of age 3 is more than half the size of the survivor zone, say 60 megabytes.

Let’s analyze: age 3 means that we survived all three GCS.

And then we think about, where did the age 3 object come from?

It must have come from age 2. No doubt about it!

Let’s think about it a little bit differently.

Now, the object of age 3 is coming from the object of age 2. (First to explain: age, only increase not decrease)

Now the age of 3 is more than halfway through the survivor zone.

Does that mean that objects of age 2 must also exceed half of the survivor zone?

Same logic: the objects of age 2 are from the objects of age 1, so there must be more than half of the objects of age 1, right? No doubt about it!

Let’s review:

In survivor, all objects of the same age are larger than the normal space of survivor. Objects older than or equal to that age can enter the old age.

You see, if it really is the rule mentioned above. More than half of the space, objects of that age or older, are aged.

What about age 3?

Because objects of age 3 are from objects of age 2, and objects of age 2 are from objects of age 1.

People who should be at age 1 when they enter the old age. You can’t fail to reach age 3 at all.

But is that really the case?

Definitely not. Let the object that exceeds the normal age of the survivor zone enter the old age directly. Are the JVM designers crazy?

Or is the book description wrong?

Here’s what really happened:

Age 1+ age 2+ age 3+ age N add up to more than half of the survivor area, and the age N and older objects enter the old age. Dynamic age judgment should look something like this. To put it more colloquically: it is the sum of the space occupied by objects from younger to older, rather than the space occupied by objects of a particular age.

Have you been cheated by the book feeling…

The following is the source code analysis:

Uint ageTable::compute_tenuring_threshold(size_t survivor_capacity) {// SURVIVor_capacity is the size of survivor space //desired_survivor_size is the dynamic age threshold to determine whether an object has entered the old age //TargetSurvivorRatio: Default 50 size_t desired_SURVIVor_size = (size_t)((((double) survivor_capacity)*TargetSurvivorRatio)/100); size_t total = 0; uint age = 1; While (age < table_size) {//sizes are sizes of all sizes // Total += sizes[age]; If (total > desired_survivor_size) break; if (total > desired_survivor_size) break; age++; } uint result = age < MaxTenuringThreshold? age : MaxTenuringThreshold; . }Copy the code

The code above confirms my suspicion:

True dynamic age judgment: the space occupied by the youngest to oldest objects is more than half of the survivor area, and then the objects equal to or older than that age are put into the old age.

summary

This is the gap between book theory and practice. If you don’t really look at the source code, you may always assume that what the book says is true. This also explains from the side: no secret source.

Supplement knowledge

  • Big object goes straight to the old age.

    • How big is a big object? An object larger than the following parameter is a large object
    • Directly through: – XX: PretenureSizeThreshold to configure
  • Dynamic age judgment

    • There’s a value in there that’s half of the survivor zone, but it doesn’t have to be half, you can customize it
    • Parameters: – XX: TargetSurvivorRatio
  • The age exceeds the threshold and enters the old age

    • This threshold: after JDK8, the range is 1-15
    • Parameters: XX: MaxTenuringThreshold