Java memory model

Source: Internet
Author: User

The Java Virtual Machine specification attempts to define a Java memory model (MODEL,JMM) that masks memory access differences between various hardware and operating systems to enable Java programs to achieve consistent memory 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.
The variables mentioned here include instance fields, static fields, and elements that make up the array object, excluding local variables and method parameters, because the latter is thread-private, not shared, and there is no competition.

Main memory vs. working memory

The Java memory model specifies that all variables are stored in the main memory, and each thread has its own working memory (working).

The working memory of a thread holds a copy of the Master memory copies of the variables used by the thread, and all operations of the thread on the variable (read, assign, and so on) must be in working memory and cannot be read directly to the variables in the main memory.

Also, there is no direct access to variables in the other's working memory between different threads, and the variables worth passing between threads need to be done through main memory, with threads, main memory, and working memory relationships such as:

Inter-memory Interactive action

The Java memory model defines the following eight actions for the specific interaction protocol between main memory and working memory, that is, how a variable is copied from main memory to working memory, how to synchronize from working memory to main memory.

    • Lock: A variable that acts on the main memory and identifies a variable as a thread-exclusive state.
    • Unlock (Unlocked): Acts on the main memory variable, releasing a variable that is in a locked state, and the released variable can be locked by another thread.
    • READ: Acts on the main memory variable, transferring a variable value from main memory to the working memory of the thread for subsequent load actions to use
    • Load: A variable that acts on working memory, which places the value of a read operation from the main memory into a variable copy of the working memory.
    • Use: A variable that acts on the working memory, passing a variable value in the working memory to the execution engine, which is performed whenever the virtual opportunity is to a bytecode instruction that needs to use the value of the variable.
    • Assign (Assignment): A variable acting on a working memory that assigns a value to a working memory from the execution engine, and performs this operation whenever the virtual opportunity is assigned to a byte-code instruction that assigns a value to a variable.
    • Store: A variable acting on a working memory that transfers the value of a variable in the working memory to the main memory for subsequent write operations.
    • Write: A variable that acts on the main memory, which transfers the store operation from the value of a variable in the working memory to a variable in the main memory.


If you want to copy a variable from main memory to working memory, you will need to follow the read and load operations, and if you synchronize the variables from the working memory back to main memory, you should perform the store and write operations sequentially. The Java memory model only requires that the above operations must be executed sequentially, without guarantee that continuous execution is required. That is, between read and load, the store and write can be inserted between the other instructions, such as the main memory of the variable A, b access, the possible order is read A,read B,load b, load a. The Java memory model also stipulates that when performing the eight basic operations above, the following rules must be met:

    • Does not allow one of the read and load, store, and write operations to appear separately
    • A thread is not allowed to discard its most recent assign operation, that is, the variable must be synchronized to main memory after it has changed in working memory.
    • A thread is not allowed to synchronize data from the working memory back to main memory for no reason (no assign operation has occurred).
    • A new variable can only be born in main memory, and it is not allowed to use a variable that is not initialized (load or assign) directly in working memory. That is, the assign and load operations must be performed before a variable is implemented with the use and store operations.
    • A variable allows only one thread to lock it at the same time, and lock and unlock must appear in pairs
    • If you perform a lock operation on a variable, the value of the variable in the working memory will be emptied, and the value of the variable will need to be re-executed before the execution engine uses the variable, either the load or the assign operation.
    • If a variable is not locked by the lock operation beforehand, it is not allowed to perform a unlock operation on it, nor is it allowed to unlock a variable that is locked by another thread.
    • Before performing a unlock operation on a variable, you must first synchronize this variable into main memory (perform store and write operations).

Java memory model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.