[JVM] On the java memory model --JMM (Java Memory Model)

In concurrent programming, what mechanism is adopted to communicate between multiple threads (exchange of information), what is the mechanism to synchronize data?
In the Java language, using the shared memory model to achieve information exchange and data synchronization between multiple threads.

By sharing a common program of state between threads, by reading - to make implicit communication write memory state public way. Refers to a mechanism for synchronizing the relative order of execution of a program in the program control between a plurality of threads on a shared memory model, the synchronization is explicit, the programmer must explicitly specify a method / code block required between the multithreaded exclusive execution.

Before saying Java memory model, let's talk about Java memory structure, that is the runtime data area:
Java Virtual machine during the execution of Java programs, the memory will divide it into several different management data area, region has its own purpose, creation time, the destruction of time.

Java runtime memory data area divided into the following areas:

1. PC register / Program Counter:
strictly speaking is a data structure that holds the memory address of the currently executing, since Java is to support multi-threaded execution, it is impossible to track the program execution is always linear actuator. When there are multiple execution threads cross, the thread of the currently executing program is interrupted address where memory is bound to be saved for use again to continue execution in accordance with the instruction address was interrupted when the interrupted thread resumes execution. To post a thread switch can be restored to the correct location of execution, each thread needs to have a separate program counter, independently of each other between threads, independent store, we call this type of memory area for the "thread private" memory, this is somewhat similar "ThreadLocal" in a way, it is thread-safe.

2. Java stack Stack Java
Java stack associated with the thread always together, whenever a thread is created, JVM will create the corresponding Java stack for the thread, in the Java stack will contain multipleStack frame (Stack Frame)These stack frames are associated with each method, each running a method creates a stack frame, each stack frame will contain some local variables, stack, and the operation information methods return values. Whenever a method execution is completed, it will pop up the stack frame element stack frame as the return value, and clears the stack frame, stack the Java stack stack frame stack activity is currently executing, that is, the current the method is being executed, PC register will point to this address. Only local variable stack frame of this event can beOperation StackIn use, when to call another method the stack frame, the corresponding a new stack frame is created, the newly created stack frame was placed top of the stack Java stack, the stack becomes the current activities. Now only the same stack of local variables can be used when the stack frame all the instructions are complete, the stack frame is removed Java stack, the stack frame just becomes active stack frame, the return value of the previous stack frame is changed an operand stack operation of the stack frame.

Because Java is a stack of correspondence with the thread, the thread Java stack data is not shared, so it does not need to be concerned about the consistency of the data, it will not lock synchronization problems.
In the Java Virtual Machine Specification, this area provides two exceptions: If the stack is greater than the depth of the thread requested virtual machine allowable depth, StackOverflowError will throw an exception; if the virtual machine can dynamically extend, if not apply to the extension enough memory, it will throw an OutOfMemoryError.

3. Heap Heap
Heap memory is the biggest piece of the JVM management, it is allThread shared, not thread-safe, is created when the JVM starts. Java objects are heap memory of the place, which is described in the Java Virtual Machine Specification is: all object instances and arrays to be allocated on the heap. Java heap is the main area of ​​the GC management, from the perspective of memory recovery point of view, because the GC is now basically using generational collection algorithm, so the Java heap can also be broken down into: the old and the new generation's; the new generation has again more carefully Eden space, From Survivor space, To Survivor space.

4. The method area
methods to load the storage areaClass information (name, modifiers, etc.), static constant class, the class information is defined as the final type Field constants, class, method class information, When passing through Class object in the program getName, isInterface other methods to obtain information, which are derived from the method area. The method area is JavaShared thread, unlike the rest of the Java heap will frequently be recovered as the GC, the information stored in its relatively stable, under certain conditions, the GC will be, when the method of the memory area to be used beyond its permitted size, will Throws OutOfMemory error of. Area is also part of the method of the heap, is what we usually refer to the permanent zone in the Java heap Permanet Generation, size can be set by parameters, you can -XX: PermSize specify the initial value, -XX: MaxPermSize specified maximum.

The constant pool Constant Pool
constant pool itself is a data structure in the method area. The constant pool is stored strings, final variable values, class names, method names, and constants. Constant pool will be determined at compile time, and stored in the compiled .class file. Generally divided into two categories: the literal and the reference amount. Is the string literal, final variables. Class and method names belong to the reference amount. The most common citations that when calling the method, find the reference method in accordance with the method name, and use it to perform the function code of conduct as a function of the body. Citations comprising: permission class interface and naming, and the name of the descriptor, the method name and descriptor fields.

6. The native method stacks Native Method Satck
native method stacks and stacks the role of Java is very similar, but the difference is the Java stack for the JVM implementation of Java service method, and the local Native method stacks for the JVM execution method service. Native method stacks and an OutOfMemoryError will be thrown StackOverFlowError.

Added:
main memory and working memory:
JMM defines all variables are stored in the main memory (Main Memory) in. Each thread also has its own working memory (Working Memory), working memory thread is saved in the main memory of a copy of a copy of the thread used variables, and thread all operations on variables (reading, assignments, etc.) have to be for working memory, and not directly read and write the main variables in memory (volatile variables still has a copy of the working memory, but because of his special predetermined sequence of operations, so it looks as if directly read and write access in the main memory in general). Between different threads can not directly access the working memory of the other variables, values are passed between threads need to be done by the main memory.

Guess you like

Origin blog.csdn.net/renjingjingya0429/article/details/89975616