Personal creation convention: I declare that all articles created are their own original, if there is any reference to any article, will be marked out, if there are omissions, welcome everyone critique. If you find online plagiarism of this article, welcome to report, and actively submit an issue to the Github warehouse, thank you for your support ~

This article refers to a large number of articles, documents and papers, but it is really complicated. My level is limited, and my understanding may not be in place. If you have any objections, please leave a comment. This series will continue to be updated, and you are welcome to comment on your questions and errors and omissions

If you like the single-part version, please visit: The most hardcore Java New Memory Model parsing and Experimental single-part version (constantly updated in QA) If you like this split version, here is the table of contents:

  • 1. What is the Java memory model
  • Analysis and experiment of the new memory model in Java – 2. Atomic access and word splitting
  • Core Understanding of the Memory barrier (CPU+ compiler)
  • The most core Java new memory model analysis and experiment – 4. Java new memory access method and experiment
  • Net most core Java new memory model analysis and experiment – 5. JVM underlying memory barrier source analysis

JMM related documents:

  • Java Language Specification Chapter 17
  • The JSR-133 Cookbook for Compiler Writers – Doug Lea’s
  • Using JDK 9 Memory Order Modes – Doug Lea’s

Memory barriers, CPU and memory model related:

  • Weak vs. Strong Memory Models
  • Memory Barriers: a Hardware View for Software Hackers
  • A Detailed Analysis of Contemporary ARM and x86 Architectures
  • Memory Model = Instruction Reordering + Store Atomicity
  • Out-of-Order Execution

X86 cpus

  • x86 wiki
  • Intel® 64 and IA-32 Architectures Software Developer Manuals
  • Formal Specification of the x86 Instruction Set Architecture

ARM CPU

  • ARM wiki
  • aarch64 Cortex-A710 Specification

Various understandings of consistency:

  • Coherence and Consistency

Aleskey’s JMM:

  • Aleksey Shipilev – Don’t misunderstand the Java Memory Model (Part 1)
  • Aleksey Shipilev – Don’t misunderstand the Java Memory Model (part 2)

Many Java developments use Java’s concurrency synchronization mechanisms, such as volatile, synchronized, and Lock. Also there are a lot of people read the JSR chapter 17 Threads and Locks (address: docs.oracle.com/javase/spec…). , including synchronization, Wait/Notify, Sleep & Yield, and memory models. But I also believe that most people like me, the first time reading, feeling is watching the fun, after reading only know that he is such a regulation, but why such regulation, not so regulation, do not have a very clear understanding. At the same time, combined with the Hotspot implementation and the interpretation of Hotspot source code, we even found that due to the static code compilation optimization of Javac and the JIT compilation optimization of C1 and C2, the final code performance was not quite consistent with our understanding of the code from the specification. In addition, such inconsistencies lead to misunderstandings when we learn the Java Memory Model (JMM) and understand the design of the Java Memory Model if we try to use actual code. I myself am constantly trying to understand the Java memory model, rereading THE JLS and various gods’ analyses. This series will sort through some of my personal insights from reading these specifications and analyzing them, as well as some experiments with JCStress, to help you understand the Java memory model and API abstraction post-Java 9. Still emphasize, however, the design of the memory model, the starting point is to let you don’t have to care about the underlying and abstracting some design, involves a lot of things, my level is limited, might not reach the designated position, understand I will try to put the arguments of the each out as well as the reference, please do not completely believe that all the views here, If you have any objections, please refute them with specific examples and leave a comment.

1. Understand “specification” and “implementation”

First of all, I would like to refer to Aleksey Shipilev’s way of understanding, that is, first of all, to make a clear distinction between Specification and Implementation. The Java Language Specification (JLS) is a Specification that specifies the Java Language, and all JDK implementations that can compile and run the Java Language must implement its specified functions. But for the actual implementation, such as Hotspot JVM JDK, is the concrete implementation, from the specification to the actual implementation, there is a certain difference. First, this code:

The actual code that HotSpot eventually compiles and runs with JIT optimisation and CPU optimisation is:

Putting the result 3 into the register and returning it is the same as the original code, and it is reasonable to omit useless local variable operations. So you might be saying, “No, when I break my point and I get to this point, I see local variables x,y,result.” If you run the JVM in DEBUG mode, the JIT will not be enabled by default and will simply interpret execution so that you can see local variables. But in practice, if the method is a hot one, these local variables will not actually exist after JIT optimization.

Another example is that Hotspot has a lock bloat mechanism (which we will test later) that says:

The operations x = 1 and y = 1 cannot be reordered as described by JLS. But the actual implementation of Hotspot optimizes the above code to:So in this case, we can actually reorder x = 1 and y = 1, which we’ll verify later.

The actual performance varies somewhat from JVM implementation to JVM implementation. And even the same JVM implementation may behave differently in different operating systems, hardware environments, and so on. Take this example:

Normally, r1 should have a value of only{1, 0}One of these two outcomes. However, execution on some 32-bit JVMS can be problematic, such as in an X86_32 environment{-1, 0, -4294967296, 4294967295}These results.

So, if we want to cover the bottom layer of JMM design and Hotspot implementation and JIT optimization and so on, there are too many things involved, one layer of logic, logic, I really can’t do it all. And I can’t guarantee that my understanding is 100% accurate. If we want to involve too much HotSpot implementation, then we may be deviated from the theme of this series, we actually our main concern is the design of the Java memory model itself specification, then summarizes in actual use, we need to know and pay attention to the minimal set of point, this is also the series to comb, at the same time, To make sure this minimal set is accurate, a lot of actual testing code is added, and you can run through it to see if the results and your understanding of the JMM are correct.

Wechat search “my programming meow” public account, add the author’s wechat, a daily brush, easy to improve skills, won a variety of offers:I will often post some good news videos of various frames of official community and add personal translation subtitles to the following address (including the above official account), welcome to pay attention:

  • Zhihu: www.zhihu.com/people/zhxh…
  • B station: space.bilibili.com/31359187