Deep parsing of Java Memory Model: summary — turn

Original address: http://www.codeceo.com/article/java-memory-7.html

Processor memory model

Sequential consistent memory model is a theoretical reference model. JMM and processor memory model usually take sequential consistent memory model as a reference in design. JMM and processor memory model will relax the sequential consistency model during design, because if the processor and JMM are implemented completely according to the sequential consistency model, many processor and compiler optimizations will be prohibited, which will have a great impact on execution performance.

According to the relaxation of the execution sequence of different types of read / write operation combinations, the memory models of common processors can be divided into the following types:

Note that the processor's relaxation of read / write operations here is based on the premise that there is no data dependency between the two operations (because the processor must comply with the as if serial semantics, the processor will not reorder the two memory operations with data dependency).

The following table shows the detailed characteristics of common processor memory models:

In this table, we can see that all processor memory models allow write read reordering. The reason is explained in Chapter 1: they all use write cache, which may lead to write read reordering. At the same time, we can see that these processor memory models allow the write of the current processor to be read earlier. The same reason is that the write cache is only visible to the current processor. This feature allows the current processor to see the write temporarily saved in its own write cache first than other processors.

The various processor memory models in the above table change from strong to weak from top to bottom. The more performance oriented processors, the weaker the memory model design. Because these processors want the memory model to have as few constraints on them as possible, so that they can do as many optimizations as possible to improve performance.

Because the common processor memory model is weaker than JMM, when generating bytecode, the java compiler will insert a memory barrier at the appropriate position of the execution instruction sequence to limit the reordering of the processor. At the same time, due to the different strengths and weaknesses of various processor memory models, in order to show a consistent memory model to different processor platforms, the number and types of memory barriers that JMM needs to insert in different processors are also different. The following figure shows the schematic diagram of the memory barrier that JMM needs to insert in different processor memory models:

As shown in the figure above, JMM shields the differences of memory models of different processors. It presents a consistent memory model for Java programmers on different processor platforms.

JMM, relationship between processor memory model and sequential consistency memory model

JMM is a language level memory model, processor memory model is a hardware level memory model, and sequential consistency memory model is a theoretical reference model. The following is the strength comparison diagram of language memory model, processor memory model and sequential consistency memory model:

From the above figure, we can see that the four common processor memory models are weaker than the three common language memory models, and the processor memory model and language memory model are weaker than the sequential consistency memory model. Like the processor memory model, the more the language pursues execution performance, the weaker the memory model design will be.

JMM design

From the perspective of JMM designers, two key factors need to be considered when designing JMM:

Because these two factors contradict each other, the core goal of jsr-133 expert group in designing JMM is to find a good balance: on the one hand, it should provide programmers with strong enough memory visibility; On the other hand, the restrictions on compilers and processors should be relaxed as much as possible. Let's take a look at how jsr-133 achieves this goal.

For specific instructions, please refer to the example code for calculating circle area mentioned earlier:

The above example code for calculating the area of a circle has three happens - before relationships:

翻译错误 TIMEOUT

JMM adopts different strategies for reordering these two different properties:

The following is the design diagram of JMM:

Two points can be seen from the above figure:

Memory visibility guarantee of JMM

The memory visibility guarantee of Java programs can be divided into the following three categories according to the program type:

The following figure shows the similarities and differences between the execution results of these three types of programs in JMM and in sequential consistency memory model:

As long as the multithreaded program is correctly synchronized, JMM ensures that the execution result of the program on any processor platform is consistent with that of the program in the sequential consistency memory model.

Jsr-133 patch to old memory model

Jsr-133 mainly fixes the old memory model before jdk5 in two ways:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>