Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a tricolor mark?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what is a three-color mark". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

[1] About Tricolor Mark

A few days ago, the company temporarily sent me to interview a java intern, because there is no experience in this area, so a careless, I asked the outline.

After asking about java basics, I casually asked again, do you know the tricolor mark?

He was obviously stunned for a moment, but only for a moment. Then, he asked with conviction, was it the red, yellow and blue mark?

That's the opposite of asking me.

Interview questions can not be answered, this is actually understandable, do not understand that do not understand, will not say that will not, the son once said, know for know.

The tricolour mark, seriously speaking, only had three colors: black, white, and gray.

But in fact, the tricolor marker has nothing to do with color, only with the state of one scan.

Black nodes represent root nodes or nodes that have been scanned, and the child nodes of this node have also been scanned;

gray nodes, representing nodes that have been scanned, and child nodes of the nodes are not scanned;

White nodes represent nodes that have not been scanned.

In the image above, A is the black node and B is gray, because B's child node C is not scanned and C is the white node.

If, at the end of the scan, C is still white, C is recycled.

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

The following conditions will occur:

B-scan complete, no reference, black.

C, logically speaking, will also turn gray and then black.

But A is already black, so its references are not scanned, so C is not scanned, it is still white.

Finally, C will be recycled as garbage.

This is obviously a mistake, since C is currently root-reachable, so what about the problem?

Common garbage collectors, CMS and G1 all give solutions.

CMS's method is called Incremental Update.

The algorithm starts from the result and judges whether there is a white object referenced by a black object at the end of scanning. If it is referenced, the black object is re-marked as gray by write barrier technology, and then scanned again.

The G1 method is called the SATB algorithm.

The algorithm starts from the source, taking snapshots before GC starts, and setting all objects that have references to be alive.

After GC scanning, a snapshot is taken again, marking the newly referenced surviving objects.

Then overlay the snapshots.

Thus, C shows that it is referenced by both objects A and B.

However, there is a drawback to this. If the reference between AC is not established, C should be recycled, but this round is not recycled.

[2] Intergenerational references

When the concept of cross-generational references was first proposed, many people felt deja vu. But to be specific, many people can't say why.

In fact, java heap in the final two generations (young generation and old generation), persistent generation in a version of jdk, was put into the local method stack.

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

In general, 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-generational references.

Cross-generational references generally occur in G1 collectors because G1 memory is chunked and memory regions are unstable.

So, when young GC is collected, should we traverse all the old age associations until the root node according to reachability analysis?

No need.

As long as the parent node is old, it is considered the root node.

Two concepts are introduced here, the result set and the card table. The card table can be seen as a collection or array of old age partitions, as shown below.

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

Think about the benefits of doing that?

[3] Safety points and safety areas

Java worker threads and garbage collection threads, in general, are not simultaneous.

Usually when the teacher talks about this problem, he will make an analogy: eating and washing dishes and cleaning tables.

The reason why worker threads and garbage collection threads cannot be performed at the same time is that people cannot clean up dishes while eating (except for tentacles).

In the same way, there is another problem. You can't take the dishes to wash in the middle of the meal.

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

This time after dinner, it was safer.

Generally speaking, a safety point is the end of a thread or the time of an interrupt, which can be a method call, a loop jump, an exception jump, etc.

Let's talk about the whole process of garbage collection.

During the execution of the service thread, a flag bit is continuously polled, and the flag bit is in the garbage collection thread;

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

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

Garbage collection thread starts to collect garbage.

So, what is the safe zone?

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

Or take the original example, if you lose weight at night, do not want to eat, dishes and so on, can be washed at any time.

How to view GC logs

Direct command: -XX+PrintGCDetails.

This command prints GC logs from the console.

The log reads as follows:

[5] Final: indicators of garbage collection

Throughput: The ratio of the time spent by an application to the total runtime of the system over the lifecycle of the application.

Formula: Throughput = System Application Time/Total System Operation Time

Garbage Collector Load: As opposed to throughput, garbage collector load is the ratio of garbage collector time to total system runtime.

Formula: Throughput = Garbage Collection Time/Total System Runtime

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

PS: Exclusive Reclaimers have long latency but high throughput, concurrent Reclaimers have low latency but low throughput.

Garbage Collection Frequency: How often the garbage collector runs.

PS: The frequency of garbage collectors should be as low as possible.

Response time: How long it takes for an object to be called garbage to be freed up.

PS: The cycle of garbage collection

over~~

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

"What is the tricolor mark" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report