I was told when a reference has been changed in jvm , using g1 gc will insert pre write barrier to change the remember set accordingly ,but where is the code ? i check the byte code implementation such as putstatic ,but i can't find where the pre barrier occur ? where and how dose the g1 gc insert the pre write barrier?
-
1Since the bytecode is implementation independent, you won’t find anything related to a particular garbage collector implementation inside it. Such barriers will be in the JVM’s interpreter and/or the code emitted by the JIT compiler. – Holger Jan 17 '20 at 14:41
-
Your terminology is wrong. What you call "pre write barrier" is actually the "post write barrier". There is also a pre write barrier in G1 but that supports SATB and doesn't touch the remembered sets. See Moinca Beckwith's slides for more details: https://www.jfokus.se/jfokus17/preso/Write-Barriers-in-Garbage-First-Garbage-Collector.pdf – user120513 Feb 22 '20 at 19:17
1 Answers
Probably already obvious from Holger's comment, but this will not be present at the bytecode level, but generated by JIT
.
The best explanation is in the source code itself, which in some case is excellent and at times very clear:
G1 also requires to keep track of object references between different regions to enable evacuation of old regions, which is done as part of mixed collections. References are tracked in remembered sets and is continuously updated as reference are written to with the help of the post-barrier.
You can also find what remembered sets are, for example, or how they are constructed.
And here is the post_barrier
method that C2
compiler uses. Though some things I can understand from that source code, some are too complicated for me, as such, happy reading the code :)

- 117,005
- 15
- 201
- 306
-
+1 but it would had been better if you had quoted the sentence before, too. In fact, what G1 maintains in remembered sets are references from old regions to young regions (needed for young and mixed collections) and references from old regions to old regions (needed for mixed collections). – user120513 Feb 22 '20 at 19:30