The Java Virtual Machine specification attempts to define a Java memory model that masks memory access differences between various hardware and operating systems, enabling Java programs to achieve consistent access across various platforms.
The main goal of the Java memory model is to define the access rules for each variable in the program, that is, the underlying details of storing variables in the virtual machine into memory and removing variables from memory.
1. Java memory Model: Java code is run on the Java Virtual machine, by the Java Virtual machine through the interpretation of execution (interpreter) or compile execution (instant compiler) to complete. Therefore, the Java memory model refers to the runtime model of the Java Virtual machine.
The memory is divided into two categories: Shared data area and thread private. (1) thread Private Zone A, program counter: A small amount of memory space, can be seen as the current thread is executed by the line number indicator of the byte code. Records the address of the virtual machine bytecode that is being executed, and each thread needs a separate program counter. If the thread is executing a Java method, this counter records the address of the executing virtual machine bytecode instruction, or null if the native method is being executed. B, Virtual machine stack: Method execution of the memory area, each method executes in the virtual machine stack to create a stack frame for storing variables table, operand stack, dynamic link, method import and export information. C, local method stack: the memory area that the virtual machine's native method executes. (2) Shared data area A, heap: The Java heap is the largest piece of memory managed by a Java virtual machine. The Java heap is an area that is shared by all threads and created when the virtual machine is started. The only purpose of this area is to hold object instances. The Java heap is a major area of garbage collector management, often referred to as a GC heap. The Java heap can be in a physically discontinuous memory space, which is logically contiguous. The size can be fixed and expandable. B, method area: Used to store the class information that has been loaded by the virtual machine, constants, static variables, the immediate compiler compiled code and other data. --Constant pool: Part of the method area that holds various literal and symbolic references generated during the compilation period. Read the book "in-depth understanding of Java virtual machines"
Java memory model