A simple introduction

The JVM memory mode refers to the memory partitions of the JVM. It is a set of specifications that require implementations of individual JVMS to comply with the JMM specification. It masks differences in memory access across hardware and operating systems so that Java programs can achieve consistent memory access across platforms.

The main function

Its main purpose is to solve the local memory data inconsistency, the compiler will reorder the code, the processor will execute the code out of order and other problems caused by the multi-thread communication through shared memory. Atomicity, visibility, and order are guaranteed in concurrent programming scenarios.

It makes it easier for developers to use these specifications to develop multithreaded programs. Without such a JMM memory model to regulate, it is likely that different virtual machines will run differently after reordering different rules for different JVMS.

Hardware level

Data consistency issues

Cache line alignmentThe CACHE at the L2 level is internal to the CPU, and each kernel corresponds to a different L2 cache. When a variable is loaded into two different L2 caches. When kernel A modifies variables, kernel B will have inconsistent parameters.

How do two L2s synchronize data before?

Out-of-order execution problems

Out-of-order execution occurs in multithreaded situations.