The introduction
In the development of computer systems, there is a huge gap between CPU computing speed and computer storage speed. In order to solve the CPU speed and speed of computer storage, the huge gap between the design personnel between the CPU and the computer storage joined the cache as a bridge between them, when in operation, data copied to the cache first, after the completion of the calculation and writes the result to computer storage, which greatly improves the computational efficiency, Avoid wasting CPU resources caused by repeated access to computer storage.
Despite this, cpus still have a lot of idle time, and multitasking was born to squeeze CPU performance. Multitasking leads to competing tasks for shared resources, which leads to concurrency problems.
In Java applications, a deep understanding of the Java memory model is necessary to better solve concurrency problems. The Java Memory model is a very important part of the Java Virtual machine, which is used to guide how the Java virtual machine works with the computer hardware memory. Before we look at the Java memory model, let’s look at what the computer hardware memory model looks like.
Hardware memory model
At the same time, there is also a cache on each CPU, but the level and location of the cache is not fixed, many modern computers cache level up to three, and may be more in the future. The location of the cache also varies, with some integrating part of the cache into the CPU.
Similarly, the cache reads and writes much faster than the computer’s main memory. Between the registers and the main memory, such a THREE-tier structure of CPU– cache — main storage constitutes the hardware memory model.
CPU in the process of program execution, often frequent calls to the same data, such as within a cycle call in another physical address function, this function may be related to the physical location of the preceding the current instruction, because of the physical memory used by a program is not continuous, resulting in the need to spend a lot of unnecessary time on the physical address. However, if the data needed to be used is read to the cache before CPU calculation, and then written to the computer’s main memory once after calculation, the resource waste caused by frequent access to the computer’s main memory can be avoided.
Java memory model
There are many similarities between the Java memory model and the hardware memory model. Due to the existence of different types of computer operating system and hardware, the physical memory model under different platforms is inconsistent. The JVM specification defines the Java Memory Model (JMM) in order to have a unified Memory access operation for Java upper development and ensure the consistency of multithreading reading and writing shared data.
The JMM also specifies that all variables are stored in MainMemory, and that each thread has its own local memory (also known as working memory), which holds a copy of the MainMemory data it needs to use. The thread reads and writes variables in local memory.
Do you see a lot of similarities between the JMM and the hardware memory model? Main memory corresponds to the computer’s main storage, and local memory corresponds to the cache. But they’re not the same thing.
Local memory is just an abstraction of the JMM, and there is no corresponding region in the JVM that can include caches, registers, and other hardware and compiler optimizations in a broad sense. This sentence sounds are difficult, we just need to know the thread to the operation of the Shared variables do not have direct access to main memory, but a visit to a middle layer, the middle layer contains the variables in the main memory copy, at the same time the middle tier access speed significantly faster than the speed of access to main memory, wrote in a definite results after the operation of the back to main memory, This greatly improves the performance of the program.
Another problem is that there is a copy of the same shared variable in every thread (for reference types, not all data), and the more threads there are, the more caching overhead there is.
JVM memory model
The JVM memory model, which defines the memory division between the thread stack and the heap, is different from the Java memory model, as explained in Understanding the Java Virtual Machine:
The two have nothing to do with it. If at all, the definitions of variables, main memory, and working memory refer primarily to the object instance data portion of the Java heap, while working memory corresponds to a portion of the virtual machine stack. At a lower level, main memory is physical memory, and for better execution, a virtual machine (or even an optimization of the hardware system itself) may prioritize working memory in registers and caches because the runtime is primarily accessing — reading and writing from — working memory.
All the primitive types (Boolean, byte, short, char, int, long, float, double) local variables are stored in the thread stack, no other threads to be Shared. The heap contains objects created in Java programs. Here’s an example:
public class MemoryModel {
public int i = 0;
public void methodOne(a) {
int localVarOne = 1;
SharedObject localVarTwo = SharedObject.sharedObject;
Integer localVarThree = new Integer(1); }}public class SharedObject {
pubic static SharedObject sharedObject = new SharedObject();
public int sharedVarOne = 1;
}
Copy the code
The local variable localVarOne in the code is stored in the thread stack. A reference to the local variable localVarTwo is stored in the thread stack, but the object itself is stored on the heap. Like localVarTwo, the reference to the local variable localVarThree is stored in the thread stack, but the object itself is stored on the heap. The difference is that when methodOne is executed by multiple threads, localVarTwo has only one piece of data in the heap due to its static type, while localVarThree has multiple pieces of data in the heap and stack. The member variable sharedVarOne of the local variable object is also stored on the heap, regardless of whether sharedVarOne is a basic or reference type. References:
- Understanding the Java Memory Model in Depth
- In-depth Understanding of the Java Virtual Machine
- The Art of Concurrent Programming in Java
- http://tutorials.jenkov.com/java-concurrency/java-memory-model.html