Abstract: Do you know tricolor marks? Is it red, yellow and blue?

This article is shared from huawei cloud community “from the three-color mark to say open”, the original author: Java junior high school students.

[1] On tricolor marking

A few days ago, the company temporarily sent me to interview a Java intern, because I don’t have any experience in this field, so I accidentally asked Chao Gang.

Asked the Java foundation, I casually asked a sentence, know the tricolor mark?

He was clearly stunned for a moment, but only a moment, and then asked rhetorically, is it red, yellow and blue?

That got me.

In fact, it is understandable that you can’t answer the questions in the interview. If you don’t understand, you should say you don’t understand. If you don’t know, you should say you won’t.

Tricolor, seriously, just black, white and gray.

But in fact, the tricolor has nothing to do with the color, it just has to do with the state of the scan.

  • The black node represents the root node or the node that has been scanned, and the child nodes of this node have also been scanned.

  • Grey nodes represent nodes that have been scanned. The child nodes of this node are not scanned.

  • White nodes represent nodes that have not been scanned.

In the figure above, A is the black node, B is gray, because the child node C of B is not scanned, and C is the white node.

If C is still white after the scan, C is recycled.

But there is a problem if, in the case above, the reference to BC is broken and the reference to AC is established, as shown below:

The following will occur:

  • B After scanning, there is no reference and the color becomes black.

  • C, logically, would also turn gray, and then black.

  • However, A is already A black node, so its reference will not be scanned, so C will not be scanned and remains white.

  • Finally, C will be garbage collected.

This is clearly a misoperation because C is currently root reachable. What about the problem?

Common garbage collectors, CMS and G1, offer solutions.

CMS’s method is called the Incremental Update algorithm.

The algorithm starts with the results and determines whether any white objects are referenced by black objects at the end of the scan. If they are referenced, the black objects are re-marked as gray through the write barrier technology, and then rescan.

G1’s method is called the SATB algorithm.

This algorithm starts at the source, takes a snapshot before GC starts, and assumes that all objects with references are alive.

After the GC scan, a snapshot is taken again to mark the newly referenced live object.

Then overlay the snapshot.

In this way, C shows that it is referenced by both objects A and B.

One drawback is that if the reference between AC’s is not established at this point, C should be recycled, but this round is not.

[2] The problem of cross-generation citation

When the concept of intergenerational citation was proposed, many people felt a sense of deja vu. But when it comes to specifics, many people are at a loss.

The Java heap is essentially just two generations (young and old), and the persistent generation is put into the local method stack after a certain version of the JDK.

Cross-generation references, where the parent node is in one generation and the reference object is in another.

Generally speaking, the parent node is in the old generation and the reference object is in the young generation.

As shown above, X references and Y references are cross-generation references.

Cross-generation references tend to occur in the G1 collector because G1’s memory is chunked and unstable.

So, should the young GC iterate through all the old age associations up to the root node based on the reachable analysis?

Don’t need.

As long as the parent node is in the old age, it is regarded as the root node.

Two concepts are introduced here (cross-generation references), result sets and card tables. A card table can be thought of as a collection or array of old age partitions, as shown below.

A result set is a set of containers similar to a map, where key stores the subscript of the card table and value stores the reference relationship

What are the benefits of doing that?

[3] Safety points and areas

Java worker threads and garbage collection threads, as a general rule, cannot run together.

Usually when teachers talk about this problem, they will use an analogy: eating and washing the dishes and wiping the table.

The reason why the worker thread and the garbage collection thread can’t work at the same time is because you can’t eat and clean up at the same time (except for tentacle monsters).

Similarly, there’s another problem. You can’t take your dishes to wash in the middle of your meal.

So, you have to do the dishes when you finish eating.

This time after dinner, it’s safer.

Generally speaking, a safe point is the time at which a thread ends or breaks, be it a method call, a loop jump, an exception jump, etc.

Let’s go back to the whole process of recycling.

  • During the execution of the business thread, it will constantly poll a flag bit, which is in the garbage collection thread;

  • If garbage collection is needed, the collection thread will change the flag bit;

  • The business thread receives the flag bit information, walks to a safe point, and stops;

  • The garbage collection thread starts to collect garbage.

So what is the safe zone?

All the points in an area are safe points, and this part is the safe area.

Again, if you’re on a diet at night and you don’t want to eat, you can always wash the dishes.

[4] How to view GC logs

Run -xx +PrintGCDetails.

This command prints GC logs on the console.

The log content is as follows:

[5] Final: indicators of garbage recycling

Throughput: The ratio of the time spent by an application over its lifetime to the total elapsed time of the system.

Formula: Throughput = system application time/total system running time

Garbage collector load: As opposed to throughput, garbage collector load refers to the ratio of garbage collector time to total system running time.

Formula: Throughput = garbage collection time/total system running time

Pause time (latency) : The application’s pause time while the garbage collector is running.

PS: Exclusive collector with long latency but high throughput, concurrent collector with less latency but low throughput.

Garbage collection frequency: How often the garbage collector runs.

PS: Garbage collector frequency should be as low as possible.

Reaction time: How long it takes for an object to be released after it is called garbage.

PS: garbage collection cycle

over~~

Heap allocation: Different garbage collectors may allocate heap memory differently. A good garbage collector should have a reasonable partition of heap memory.

In this paper, the resources: blog.csdn.net/xingkongjuh…

Click to follow, the first time to learn about Huawei cloud fresh technology ~