Java memory model

Source: Internet
Author: User

Main memory vs. working memory

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 variable here (Variable) is slightly different from the variable described in Java programming, which includes instance fields, static fields, and elements that make up the array object, but excludes local variables and method parameters, because the latter is thread-private and not shared, and there is no competition problem naturally. To achieve better performance, the Java memory model does not restrict the execution engine from using the processor's specific registers or caches to interact with the main memory, nor does it limit the right of the immediate compiler to adjust the order of code execution.

The Java memory model specifies that all variables are stored in main memory (Mainmemory) (where the main memory is the same as the main memory name when the physical hardware is introduced, the two can be compared to each other, but this is only part of the virtual machine memory). Each thread also has its own working memory (workingmemory, which can be compared to the processor cache analogy described above), where the thread's working memory holds a copy of the master memory of the variable used by the thread, and all operations of the thread on the variable (read, assign, etc.) must be in working memory. You cannot read and write directly to the variables in main memory. There is no direct access to variables in the other's working memory between different threads, and the transfer of variable values between threads needs to be done through main memory, as shown in the interaction between threads, main memory, and working memory.

Figure 1 The interaction between threads, main memory, and working memory

The main memory, working memory, and the Java heap, stack, method area in the Java memory area described in Chapter 2nd of this book are not the same level of memory division. If the two must be forced to correspond, that from the definition of variables, main memory, and working memory, main memory mainly corresponds to the instance data portion of the object in the Java heap [5], while the working memory corresponds to some areas of the virtual machine stack. At a lower level, the main memory is the memory of the hardware, and in order to get better running speed, the virtual machine and hardware system may make the working memory priority in the register and cache.

Inter-memory interaction operations

The following eight actions are defined in the Java memory model for the implementation details of the specific interaction protocol between main memory and working memory, i.e. how a variable is copied from main memory to working memory, and how to synchronize from the working memory back to main memory:

    • Lock: A variable that acts on the main memory, which identifies a variable as a thread-exclusive state.
    • Unlock (Unlocked): A variable that acts on the main memory, releasing a variable that is in a locked state, and the released variable can be locked by another thread.
    • READ: A variable that acts on the main memory, transferring the value of a variable 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 acting on a working memory that passes the value of a variable 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 puts the value of a store operation's variable from the working memory into a variable in the main memory.

If you want to copy a variable from main memory to working memory, perform the read and load operations sequentially, and if you want to synchronize the variables from the working memory back to the main memory, execute the store and write operations sequentially. Note that the Java memory model requires only that the two operations must be executed sequentially, without the assurance that sequential execution must be performed. That is, between read and load, store and write can be inserted into other instructions, such as the main memory of variables a, b access, a possible order is Reada, readb, LOADB, Loada. In addition, the Java memory model also stipulates that the following rules must be met when performing the eight basic operations described above:

    • One of the read and load, store, and write operations is not allowed to appear separately, that is, a variable is not allowed to read from the main memory but the working memory is not accepted, or from the working memory initiated writeback but the main memory does not accept the situation.
    • A thread is not allowed to discard its most recent assign operation, that is, the variable must be synchronized back to main memory after it has changed in working memory.
    • A thread is not allowed to synchronize data from the working memory of the thread back to main memory for no reason (no assign operation has occurred).
    • A new variable can only be "born" in main memory, does not allow direct use of a variable that is not initialized (load or assign) in working memory, in other words, the Assign and load operations must be performed prior to implementing a use and store operation on a variable.
    • A variable allows only one thread to lock it at the same time, but the lock operation can be repeated multiple times by the same thread, and the variable will be unlocked only after performing the same number of unlock operations, after performing the lock multiple times.
    • If you perform a lock operation on a variable, the value of this variable in the working memory will be emptied, and the value of the variable initialized by the load or assign operation needs to be re-executed before the execution engine uses the variable.
    • 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 back into main memory (performing sotre and write operations).
Resources

Zhou Zhiming:< in-depth understanding of Java Virtual machines >

Deep understanding of Java memory Model (i)--Basics: Http://www.infoq.com/cn/articles/java-memory-model-1

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.