Java Memory Model

(updated: )
  1. 1. Areas
    1. 1.1. Shared Amoung Threads
      1. 1.1.1. Method Area
      2. 1.1.2. Constant Pool
      3. 1.1.3. Heap
      4. 1.1.4. Direct Native Memory
    2. 1.2. Not Shared Amoung Threads
      1. 1.2.1. Stack
      2. 1.2.2. Native Method Stack
      3. 1.2.3. Program Counter Register
  2. 2. Reorderings
    1. 2.1. Lock and Unlock
    2. 2.2. Volatiles and Monitors
    3. 2.3. Final Fields
  3. 3. See Also

Areas

memory model

Shared Amoung Threads

Method Area

It stores following information, which are loaded by classloaders.

  • class information
  • the bytecode of constructors and methods
  • a runtime constant pool per classloader
JDK Method Area
Oracle JDK < 8 PermGen
Oracle JDK >= 8 MetaSpace

Constant Pool

A subpart of Method Area.

When a class, method or field is referred to, the JVM searches the actual address in the memory by using the runtime constant pool. It also contains constant values like string litterals or constant primitives.

Heap

All class instances and arrays are allocated in the Heap.

Direct Native Memory

Can be allocated by DirectByteBuffer.

Direct Native Memory is allocated out side of Heap, while contains similar data as Heap, but you have to release the memory manually, since no GC for Direct Native Memory.

It’s been employed to reduce the copying between native memory and Heap.

Not Shared Amoung Threads

Stack

Stack stores multiplies Frames, where a frame is a data structure that contains multiplies data that represents the state of the thread of current method.

Native Method Stack

Stack for native methods, which is controlled by underlying OS.

Program Counter Register

Each thread has its own PC Register, which is created the same time along with the thread.

It contains the JVM instruction (in Method Area) currently being executed.

Reorderings

Reordering happens due to the out-of-order executions in CPUs and compilers, while JMM consists of the following rules to disallow reorderings.

Lock and Unlock

Including synchronized methods or blocks,

Volatiles and Monitors

  
Can Reorder 2nd operation
1st operation Normal Load / Normal Store Volatile Load / MonitorEnter Volatile Store / MonitorExit
Normal Load / Normal Store No
Volatile Load / MonitorEnter No No No
Volatile store / MonitorExit No No

Where:

  • Normal Loads are getfield, getstatic, array load of non-volatile fields.
  • Normal Stores are putfield, putstatic, array store of non-volatile fields
  • Volatile Loads are getfield, getstatic of volatile fields that are accessible by multiple threads
  • Volatile Stores are putfield, putstatic of volatile fields that are accessible by multiple threads
  • MonitorEnters (including entry to synchronized methods) are for lock objects accessible by multiple threads.
  • MonitorExits (including exit from synchronized methods) are for lock objects accessible by multiple threads.

Final Fields

The initialization of final fields in constructor are guarenteed to happends-before exiting the constructor.

See Also