Home Java javaTutorial In-depth analysis of Java memory model: summary

In-depth analysis of Java memory model: summary

Dec 29, 2016 am 11:27 AM

Processor Memory Model

The sequential consistency memory model is a theoretical reference model. The sequential consistency memory model is usually used as a reference when designing JMM and processor memory models. The JMM and processor memory model will make some relaxations in the sequential consistency model when designing, because if the processor and JMM are implemented completely in accordance with the sequential consistency model, many processor and compiler optimizations will be prohibited, which is detrimental to Execution performance will have a big impact.

According to the relaxation of the execution order of different types of read/write operation combinations, the memory models of common processors can be divided into the following types:
Relax the order of write-read operations in the program, by This resulted in the total store ordering memory model (TSO for short).
Based on the previous 1, we continue to relax the order of write-write operations in the program, resulting in the partial store order memory model (referred to as PSO).
Based on the previous 1 and 2, we continue to relax the order of read-write and read-read operations in the program, resulting in the relaxed memory order memory model (referred to as RMO) and the PowerPC memory model.

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 as-if-serial semantics, the processor does not Two memory operations with data dependencies will be reordered).

The following table shows the detailed characteristics of common processor memory models:
Memory model name
Corresponding processor
Store-Load reordering
Store-Store reordering
Load-Load and Load-Store reordering
Writes from other processors can be read earlier
Writes from the current processor can be read earlier
TSO
sparc-TSOX64
Y
Y
PSO
sparc-PSO
Y
Y
Y
RMO
ia64
Y
Y
Y

Y
PowerPC
PowerPC
Y
Y
Y
Y
Y
In this table we can see all the processor memory The models all allow write-read reordering for the reason explained in Chapter 1: they all use write buffers, which may cause write-read operations to be reordered. At the same time, we can see that these processor memory models allow earlier reads of writes to the current processor. The reason is also because of the write cache area: Since the write cache area is only visible to the current processor, this feature causes the current processor to be able to Other processors see writes that are temporarily held in their own write buffers first.

The various processor memory models in the table above, from top to bottom, the models change from strong to weak. The more performance-focused a processor is, the weaker the memory model will be. Because these processors want the memory model to constrain them as little as possible so that they can do as many optimizations as possible to improve performance.

Since the common processor memory model is weaker than the JMM, when the Java compiler generates bytecode, it will insert memory barriers at appropriate locations in the execution instruction sequence to limit processor reordering. At the same time, since the memory models of various processors have different strengths and weaknesses, in order to present a consistent memory model to programmers on different processor platforms, the number and types of memory barriers that JMM needs to insert in different processors also vary. Are not the same. The following figure shows a schematic diagram of the memory barriers that JMM needs to insert in different processor memory models:

In-depth analysis of Java memory model: summary

As shown in the figure above, JMM masks the differences in different processor memory models. , which presents Java programmers with a consistent memory model across different processor platforms.

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

JMM is a language-level memory model, and the processor memory model is a hardware-level memory model with consistent sequence The sexual memory model is a theoretical reference model. The following is a diagram comparing the strengths and weaknesses of the language memory model, processor memory model and sequential consistency memory model:

In-depth analysis of Java memory model: summary

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

Design of JMM

From the perspective of a JMM designer, two key factors need to be considered when designing JMM:
The programmer's use of the memory model. Programmers want memory models that are easy to understand and easy to program. Programmers want to write code based on a strong memory model.
Compiler and processor implementation of the memory model. Compilers and processors want the memory model to constrain them as little as possible so that they can do as many optimizations as possible to improve performance. Compilers and processors want to implement a weak memory model.

Since these two factors are contradictory, the core goal of the JSR-133 expert group when designing JMM is to find a good balance point: on the one hand, it must provide programmers with a strong enough memory visibility guarantee; On the other hand, compiler and processor restrictions should be relaxed as much as possible. Let's take a look at how JSR-133 achieves this goal.

For specific explanation, please see the sample code for calculating the area of ​​a circle mentioned earlier:

double pi  = 3.14;    //A
double r   = 1.0;     //B
double area = pi * r * r; //C
Copy after login

The above sample code for calculating the area of ​​a circle has three happens-before relationships:
A happens- before B;
B happens- before C;
A happens- before C;

Since A happens- before B, the definition of happens- before will require: the result of the execution of operation A It should be visible to B, and the execution order of A operation should be before B operation. However, from the perspective of program semantics, reordering A and B will not change the execution results of the program, but can also improve the execution performance of the program (allowing this reordering reduces the constraints on compiler and processor optimization ). In other words, among the above three happens-before relationships, although 2 and 3 are necessary, 1 is unnecessary. Therefore, JMM divides the reordering prohibited by happens-before requirements into the following two categories:
Reordering that will change the program execution results.
Will not change the reordering of program execution results.

JMM adopts different strategies for these two different natures of reordering:
For reordering that will change the execution results of the program, JMM requires the compiler and processor to prohibit such reordering.
JMM does not place requirements on the compiler and processor for reordering that does not change the execution results of the program (JMM allows such reordering).

The following is the design diagram of JMM:

In-depth analysis of Java memory model: summary

Two points can be seen from the above picture:
The happens-before rules provided by JMM to programmers Can meet the needs of programmers. JMM's happens-before rules are not only simple and easy to understand, but also provide programmers with strong enough memory visibility guarantees (some memory visibility guarantees do not necessarily actually exist, such as A happens-before B above).
JMM has as few constraints on the compiler and processor as possible. From the above analysis, we can see that JMM actually follows a basic principle: as long as the execution results of the program are not changed (referring to single-threaded programs and correctly synchronized multi-threaded programs), the compiler and processor can be optimized no matter how they are used. . For example, if the compiler determines after careful analysis that a lock will only be accessed by a single thread, the lock can be eliminated. For another example, if the compiler determines after careful analysis that a volatile variable will only be accessed by a single thread, then the compiler can treat the volatile variable as an ordinary variable. These optimizations will not change the execution results of the program, but also improve the execution efficiency of the program.

JMM's memory visibility guarantee

The memory visibility guarantee of Java programs can be divided into the following three categories according to program type:
Single-threaded programs. Single-threaded programs do not have memory visibility issues. The compiler, runtime, and processor work together to ensure that the execution results of a single-threaded program are the same as the program's execution results in the sequential consistency model.
Correctly synchronized multi-threaded programs. The execution of a properly synchronized multithreaded program will have sequential consistency (the results of the program's execution will be the same as if the program were executed in a sequentially consistent memory model). This is the focus of JMM, which provides programmers with memory visibility guarantees by limiting compiler and processor reordering.
Unsynchronized/incorrectly synchronized multi-threaded programs. JMM provides them with minimal safety guarantees: the value read when a thread executes is either the value written by a previous thread or the default value (0, null, false).

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

In-depth analysis of Java memory model: summary

As long as the multi-threaded program is correctly synchronized, JMM guarantees that the execution results of the program on any processor platform are consistent with the execution results of the program in the sequentially consistent memory model.

JSR-133's fixes to the old memory model

JSR-133's fixes to the old memory model before JDK5 mainly include two:
Enhance volatile memory semantics. The old memory model allowed volatile variables to be reordered with ordinary variables. JSR-133 strictly limits the reordering of volatile variables and ordinary variables, so that volatile write-read and lock release-acquire have the same memory semantics.
Enhance the memory semantics of final. In the old memory model, the value of the same final variable may be different when read multiple times. To this end, JSR-133 adds two reordering rules for final. Final now has initialization safety.

The above is the in-depth analysis of Java memory model: summary. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles