This is why Technology’s 37th original article

As usual, let’s talk about life. I took the picture above on Monday.

Monday night after work to find the company downstairs with a tricycle selling flowers aunt started selling flowers again. She was the only one doing business at the intersection, and there were few pedestrians on the road. Everyone hurried along with their heads bowed, with a little sadness in the flowers.

So I went and bought a handful of white roses.

Last Sunday, after watching love in the Time of Cholera, I just took the above photo as a prop. I don’t like the debonair story as a whole, and I don’t like the plausible excuse that “I’ve had 622 lovers in my life, but I’ve only loved you.” Although it really does exhaust all the possibilities of love, it is not real enough.

By contrast, I think qian Zhongshu wrote “Fortress Besieged” : “WHEN I say let her three parts, not three parts of water and seven parts of dust, but three parts of the full moon. This kind of rough love is more real.

Look at The three of Us by Yang Jiang. At the end of the book, she said, “Good things in the world are not firm, and colored clouds are easy to scatter and colored glass is brittle.” This is love. This is real life.

All right, back to the article.

I’m sorry. I was wrong.

The two previous posts:

Interviewer: You say you are familiar with the JVM? Tell me about concurrency accessibility analysis.

Interviewer: How does the G1 Collector Know When You’re Garbage?

There are some things that are not clear, and a lot of readers have asked, so I think I need to add a few caveats.

More importantly, after expert guidance, there are still some wrong descriptions, I also need to correct.


If it had been an interview question, the interviewer would have said to me, ok, let’s stop here for today. I want you to go back and wait.


If you have not read the two articles I just said, I suggest you do not read this one, because a look will have to see three, if the derivative knowledge points you still want to thoroughly understand, an afternoon in the past…… (Of course, you’ll still get something.)

If you read my previous two articles, I beg you must read this one, supplement, correct the answer, such as the interviewer really asked for details, also not afraid……


Well, before reading this article, I’m assuming you’ve read the two excellent, humorous, and informative articles I mentioned earlier.

Concurrency accessibility analysis – Errata

In the article interviewer: You say you are familiar with JVM? Would you like to talk about concurrent reachability analysis?


Let me bring up this GIF:


First of all, to be clear, I don’t understand this GIF anymore either. (A wrong picture is a wrong picture, but also forced to find a reason).

Next, forget The GIF and re-analyze a wave of original Snapshot scenarios.


First, let’s look at the object diagram when the GC thread starts scanning after the initial marking phase (root node enumeration) is complete and just after entering the concurrent marking phase:


In the above image, the object map is determined when GC Roots are determined. SATB scanning is based on the determined object graph (the object graph of the snapshot version). In other words, the reference relationship of the snapshot graph will not change during the scanning, but the real object graph will change.

For example, if you take a picture in the playground, you count the number of people in the picture, the picture will not change, the number of people is always the same, but the real people in the playground change from moment to moment.

So, at the moment the object graph is determined, after normal scanning is complete, the object graph looks like this:


All right, that’s the setup.

What we need to demonstrate here is the “object disappearing” case.

First, let’s determine the object graph shown above. There must be a moment in the concurrent marking phase when the object graph looks like this:


Let’s talk about “object disappearance” based on this object graph at this moment.

Remember that both conditions must be met for the object to disappear? (These two conditions are taken from Understanding the Java Virtual Machine in Depth (3rd edition) P.89)

Condition 1: The assignor inserts one or more new references from black objects to white objects.

Condition 2: The assignment removes all direct or indirect references from a gray object to the white object.

If we read the second condition again carefully, you will see that it says ** “the white object”. This “the white object” refers to the white object in condition 1. **

Therefore, we have reason to believe that condition 1 and condition 2 are sequential, that is, the assignment must insert one or more new references from the black object to the white object, and then the assignment must delete all direct or indirect references from the gray object to the white object. In this case, the object disappears.

After the guidance of a superior person, we can also carry out proof by contradiction, as follows:

We assume that the reference from the gray object to the white object is deleted first, that is, condition two is triggered first. The real object graph at the corresponding time would look like this:

(Note that I emphasize the real object graph here, not the snapshot object graph. Again: The object graph of the snapshot is determined at the beginning of the scan and does not change during the scan.


Then, if the white object 9 is in a free state, not connected by any reference chain from the Root node, which in graph theory is unreachable from GC Root to object 9, then this object is no longer possible to be used. So the user thread can’t point black object 5 to free white object 9, you can’t write code like that.

If you didn’t see the above picture at first glance, then take a look at the following picture and you’ll see:


Black object 5 cannot point to white object 9, then rule 1 fails.

Therefore, to sum up, we can conclude that condition one and condition two are in sequence.

Then we continue to make the figure as follows according to condition 1:


Condition one is that the assignor inserts one or more new references from the black object to the white object.

In the scenario shown above, the GC thread is working while the assignment inserts a new reference between black object 5 and white object 9. (Marked by a red line)

At this point, since gray object 6 points to white object 9, black object 5 can point to white object 9. Think of our previous proof that black objects can reach white objects as long as there is a reference chain.

At this point, only condition one is satisfied, and the object hasn’t disappeared yet.

Here is the diagram of condition two, which STAB destroys:


Condition two is that the assignment removes all direct or indirect references from the gray object to the white object.

In the scenario shown in the figure above, it is the assigner that removes the direct reference to grey objects 6 through white objects 9.

At this point, white object 9 is the “missing object” because black object 5 is not scanned again.

It is important to note that the assignment can be understood as a user thread. Since the user thread and the GC thread are running at the same time during the concurrent marking phase, the above figure is required, as well as a precondition:

The user thread removes references between objects 6 and 9 before the GC thread grays object 6. Only then will the GC thread have the corresponding write barrier record when it reaches object 6.

6 if has scanned in the GC thread object, namely the object is black 6 cases (9 this time object, not black is gray, can’t be white), user threads to remove objects from six to nine references between objects, the GC thread is not need to deal with, because the object 9 is white, it is bound to survive in this round.

Here I quote the description of R big:

https://hllvm-group.iteye.com/group/topic/44381?page=2

Because deletion triggers a pre-write barrier, the old reference values are recorded every time the reference relationship changes, so that by the time the GC thread reaches an object, all the changes in the reference type fields of that object have been logged, so that no objects alive in the snapshot graph have been missed. Of course, it is possible that an object is alive in the snapshot, but as the concurrent GC progresses it may already be dead, but SATB will let it live through the GC and become floating garbage.

SATB in the write barrier, the old reference to the object is not white (do not care, or white to become gray).

The practical effect of this is that if a gray object’s field originally points to a white object but is assigned another value (say null) before concurrent Marker can scan the field, the association between the field and the white object is cut off. The SATB write barrier ensures that the object referenced by the field is grayed out before the cut-off occurs, thereby preventing the occurrence of condition 2.

Among them: “Make all objects referred to by the old reference non-white.” In our scenario, the implications are as follows:

Old references refer to references between grey object 6 and white object 9.

The object pointed to refers to: white object 9.

All non-white: refers to the fact that the white object 9 becomes gray.

So, after the two conditions are triggered in sequence and the object graph scan is complete, it looks like this:


After the concurrent scan is complete, scan again with the gray object 9 as the root (it will naturally turn black as the root), so the final object graph looks like this:


What would the object graph look like if the user thread did not point object 5 to object 9 during the marking process, but only deleted references between object 6 and object 9?

It looks like this, which you can probably imagine:


Object 9 is still black, but it becomes floating garbage and escapes this collection. Does not affect the program operation.

Next, let the above image move, and I slow down the order in which I switch between images. You can taste it yourself:


Therefore, all the above description is the correct one I think, showing how SATB solution solves the problem of “object disappearance” process.

The previous description of this section in the Interviewer: You said you are familiar with the JVM? What would you like to talk about concurrent reacability analysis? Is too simple and contains errors.

When were you garbage – Erratum

In G1 Recycler: How Do I Know When You’re Garbage? Here’s a description from the article:


“Objects to which GC Roots can be directly associated: parts of a Region that have already been used, so between bottom and top.” This sentence is wrong.

In fact, you can see it in the description later in the article. The collection of objects that GC Roots can be directly associated with should be “smaller” than the already used part of the Region. After the object graph is recursed, the sum of all objects is equal to the already used part of the Region.

From the picture in the latter part of the article, it can be intuitively found that the Region from bottom to top is already used. However, only objects between bottom and NextTAMS are directly associated with GC Roots. These objects are not already used by a Region.


When were you garbage? – Footnote

About G1 Recycler: How Do I Know When You’re Garbage? There are two caveats to this article.

Some readers ask: the article does not discuss the content of recycling, each cleaning does not really recycle, so does it happen after several rounds of marking?

one

First of all, there really is no discussion of recycling in the article. I also wrote in the previous section that G1 is recycled and divided into two parts:

1.Global Concurrent Marking: Global concurrency Marking.

Evacuation Pauses: This stage is responsible for copying live objects from a part of a Region into an empty Region, and then reextracting the original Region space.

The question posed by the article can be answered by understanding the global concurrent marking phase:


So I just showed the global concurrent marking phase.

If you want to learn more about the recycling phase, you can check out R’s answers. It is highly recommended that you read this article, give it a thumbs up, open the link below, and read it several times:

https://hllvm-group.iteye.com/group/topic/44381

Second, “Each cleanup does not actually recycle, so does one recycle occur after multiple rounds of marking?”

This is probably because of my emphasis on not copying any objects during the cleanup phase, and the fact that I didn’t describe the recycle phase.

A global concurrent tag is followed by a collection.

It’s just that the G1 collector can model predictable pause times (-xx :MaxGCPauseMillis specified, with a default value of 200 milliseconds) because it uses regions as the smallest unit of a single collection, that is, each collection of memory is an integer multiple of Region size. This allows you to systematically avoid region-wide garbage collection across the entire Java heap.

To be more specific, each Region has a “value” for garbage (the value is the amount of space collected and the amount of time it takes to collect). These “values,” maintained in a priority list, are known to the G1 collector.

Therefore, in the reclamation phase, regions with the highest reclamation value are processed first. Therefore, a single collection does not reclaim all regions.

two

This also explains another question raised by the reader: if the red box is recycled after each mark, why is it that the red box is in the same area as the last mark, as if it had not been cleaned up? Shouldn’t there be no space left after the mark?


I think a reasonable explanation is what I said above: the value of this Region is not enough, so it is not reclaimed this time. As time goes on and more and more rubbish accumulates in it, the “value” gets higher and higher and it is always recycled.

Another reader asked: What does prevBitmap do after seeing the process of concurrent tags? Because it feels like I’m scanning from scratch every time, and I don’t see what it’s doing.

This question can be answered by starting with this picture:


The E is Remark phase, and you can see that PrevBitmap is used in this phase.

The prevBitmap Region does not need to be marked because the Region is garbage. The prevBitmap Region does not need to be marked because the Region is garbage.

We can assume that E represents the Remark stage of the process of the NTH round of recycling. So PrevBitmap is the token result for round N-1.

As stated in the previous article, a previous Bitmap records the state of the object Marking after the previous round of Concurrent Marking, and since the previous round was round N-1, the Bitmap information can be used directly.

The range of addresses recorded in it does not need to be marked again, because the object corresponding to these addresses is already garbage.

In figure F, it can be seen that the current figure F is the state in which the cleaning phase has been completed:


There are two criteria for judging:

1. PrevBitmap and NextBitmap have swapped places compared to E.

2. The PrevBitmap address space has been marked light gray.

At this point the tag is complete, and the PrevBitmap becomes the result of the n-1 tag.

What kind of trash are you? – A douchebag

Since the previous article has been published, I need to modify the corresponding content. Note to the following readers that if they see the article, they need to be aware of the problems described in these areas.

But IN the process of searching my article, I found some things that made me very depressed. Most of my previous articles were plagiarized. I also see no wonder.

The most egregious is this:


This is a hundred account, verbatim copy of my articles, but also marked as “original”?

I went and wrote a review:


He didn’t dare put the review out yet.


And this one down here, you can count on it. I’d like to take this picture at home, but I can’t:


There are many more cases like this. In the end, it comes down to copyright awareness.

I talked about copyright issues in the article “Subscription number 77 days, I earned 487.52 yuan” :


My account will not distribute any pirated resources, it has never been, it is, and it will be.

The greatest good is to do no evil. With your mutual encouragement.

So I hereby solemnly declare that if I reprint my article without permission, I must indicate the original address, and keep the qr code of the public number at the end of the article, otherwise I will see a report a.


I first report you involved in pornography, attract the attention of the staff, and then report you plagiarism, let the staff punish you.

I’m so mad.


One last word (for attention)

Through this incident, I also felt again that when reading wild articles on the Internet (such as mine), we should hold a cautious, suspicious and learning attitude.


If you find something wrong, please leave a message and point it out to me so that I can modify it. (I have this quote in every technical article, and I mean it.)

Thank you for reading, I insist on original, very welcome and thank you for your attention.

I am why Technology, a nice sichuan man who is not a big shot, but likes to share, warm and interesting.

The above.

Welcome to pay attention to the public account [WHY Technology], adhere to the output of original. Share technology, taste life, wish you and I progress together.