JVM memory structure and memory model

A, Jvm memory structure

Modular decomposition

1. The program counter (thread-private)

What (1)?

    The program counter is the current thread bytecode executed by the line number indicator.

(2) the role?

    Bytecode interpreter bytecode instructions at selected by changing the value of a need to perform this calculator, branched, cyclic, jump, exception handling, thread resumes, if the Native method is performed, the value of this calculator Is empty

2. Java Virtual Machine stack (thread-private)

What (1) is

    Java Virtual Machine stack Java memory model described method performed: Each method creates a stack frame at the same time the implementation of each method from the process until the execution is completed calls, it corresponds to a stack frame into a virtual machine stack a stack to process stack.

(2) Composition

① local variable table

    Storage of the basic types of known compilation of object reference type and returnAddress type (a point to the address of the bytecode instructions. Bytecode instructions i.e. the program is stored in the method area, and the value is a pointer type returnAddress specific instruction memory address pointer)

② operand stack

    Operand is a last in, first out stack, the JVM opcodes are all data on the operand stack operates, for each call a method, the JVM creates a stack operand to use for the calculation.

    For example, during the execution of a = b + c bytecode operand stack and local variable table changes as shown below.

    Stored in the local variable table A, b, c three local variables, b and c, respectively, the first stack

③ Dynamic Link

    During the conversion operation is a direct reference, it is called dynamic linking. Constant holding Class bytecode contains a lot of symbol references, only at runtime reference becomes a direct reference symbol (i.e. data points), or a method may be a reference field.

④ for export

    That is the next step of the method after the address of the instruction, the method exits normally, the caller can counter value of the PC as a return address, when the abnormal exit, the return address is to be determined by the exception handler.

3. The native method stacks (thread-private)

What (1)?

    Save the native method to enter the address of the area

4. Java heap (threads share)

What (1)?

    All object instances and arrays to be allocated on the heap. The main area is managed by the garbage collector.

(2) partition

① new generation: Eden area, Survivor From zone, Survivor To region
② years old

The method area (threads share)

What (1) is

    Method class information area for storing the virtual machine has been loaded, a method, a constant, static member variable, the JIT (time compiler) data such as the compiled code, class loading during distribution.

(2) also known as non-heap space allocated yuan

    Spatial metadata: metadata class, method data, the method information (byte code, stack and variable size), runtime constant pool, the determined reference symbols and the like vtable zone, stored in the local memory area (external memory heap)

6. runtime constant pool (shared thread)

What (1) is

    Runtime constant pool of the various symbols and literals for storing compile and run generated reference. This section will run into the method area after class loading time constant storage pool. The method is part of the region

(2) and a literal reference symbol

    . Literal: string 1; 2 primitive values; 3.final constant symbolic references: 1. the fully qualified class name and method; 2 field names and descriptors; 3 name of the method.

Two, Jvm memory model

1. Definitions

    Due to differences platform memory model, it may cause an error in concurrent access to different platforms. Java memory model (Java Memory Model, JMM) is shielded variety of hardware and operating system differences in memory access, in order to achieve let Java programs in a variety of platforms to achieve the same effect memory access.

2. Specific operation

    Program access rules define the variables, i.e., variables in the virtual machine and stored in memory in the details of the underlying variables removed from memory.
    Variable here means instance fields, static fields and array elements constituting the object, the method does not include local variables and parameters

3. main memory and working memory

    Jvm memory model requires all variables are stored in the main memory, each thread also has its own working memory, working memory holds a copy of a copy of main memory to be used by the thread of variables.

4. synchronization、final、volatile

(1) synchronization

① mutually exclusive

    For a monitor object when held by a thread, other threads can only wait

② Visibility

    Guaranteed write operation during thread synchronization code blocks for subsequent thread enters the code block is visible (the same holds thread monitor object).
    Monitor the current thread releases the object, role of the cpu cache refresh data to the main memory; other threads enter the code block needs to acquire monitor object, a cache miss will cpu, so that variable is reloaded from main memory.

③ prohibition instruction reordering

(2) final

① prohibition instruction reordering
② Visibility

    The modified final field once initialization is complete in the constructor, and the constructor did not "this" reference pass out ( "this" reference escape is a very dangerous thing, there are other threads may access through this reference to "initialized half "of the object), then other threads can see the value of the final field.

(3) volatile

① Visibility
② prohibit instruction reordering

Example 5

(1) reordering

(2) visibility

(3) mutually exclusive

Guess you like

Origin juejin.im/post/5d62815bf265da03b638b7b1