Java memory model

Source: Internet
Author: User

1. Overview

Multitasking and high concurrency are one of the most important indicators of the ability to measure a computer's processor. A general measure of the performance of a server, the use of transactions per second (transactions per Second,tps) This indicator comparison can explain the problem, it represents the average number of servers in a second to respond to requests, and the TPS value and program concurrency is very closely related. Before discussing the Java memory model and threading, let's briefly describe the efficiency and consistency of the hardware.

2. The efficiency and consistency of the hardware

Since there are several orders of magnitude difference between the computer's storage device and the processor's computing power, modern computer systems have to add a cache of read-write speeds as close as possible to the processor's operating speed as a buffer between memory and processor: the data to be used for the operation is copied to the cache. So that the operation can be done quickly, when the operation is finished and then synchronized back to the memory from the cache without waiting for slow memory to read and write.
Cache-based storage interaction solves the contradiction between processor and memory speed, but introduces a new problem: Cache consistency (Coherence). In multiprocessor systems, each processor has its own cache, and they share the same main memory, as shown in: Multiple processor operations involve the same piece of main memory, requiring a protocol that guarantees data consistency, such as MSI, MESI, Mosi, and Dragon protocol. The memory access operations defined in the Java Virtual Machine memory model are comparable to the cache access operations of the hardware, and the Java memory model is described later.

In addition, in order to allow the processor inside the operating unit can be fully utilized, the processor may be in the input code execution (Out-of-order execution) optimization, the processor will be after the calculation of the disorderly execution of the code to reorganize the results to ensure the accuracy of the results. Similar to the processor's unordered execution optimizations, the Java Virtual machine's instant compiler also has similar command reordering (instruction Recorder) optimizations.

3.Java memory model

Defining the Java memory model is not an easy task, and the model must be sufficiently rigorous to allow Java concurrency to be ambiguous, but it must be loose enough to allow the virtual machine's implementation to have enough free space to take advantage of the various features of the hardware (registers, cache, etc.) for better execution speed. After a long period of validation and patching, the Java memory model has matured and perfected after JDK1.5 was released.

3.1 Main memory vs. working memory

The main goal of the Java memory model is to define access rules for variables in the program, that is, the underlying details of storing variables into memory and removing variables from memory in the virtual machine. The variables here are different from those described in Java programming, which include instance fields, static fields, and elements that make up an array object, but do not include local variables and method parameters, which are thread-private and not shared.

The Java memory model specifies that all variables are stored in main memory, each thread has its own working memory (which can be compared to the cache of the processor before it), that the thread's working memory holds the variable used by the thread to the master memory copy, and all the operations of the thread on the variable (read, Assignment) must be done in working memory and not directly read and write to the variables in main memory. Variables in the other's working memory cannot be accessed directly between different threads, and the transfer of variable values between threads needs to be done in main memory, with threads, main memory, and working memory interacting as shown, and very similar.

The main memory, the working memory, and the Java heap, stack, and method area of the Java memory area are not the same level of memory partitioning.

3.2 Inter-memory interoperability

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).
3.3 Re-ordering

In order to improve performance when executing programs, the compiler and processor often reorder instructions. Reordering is divided into three categories:

    1. The re-ordering of compiler optimizations. The compiler can rearrange the execution order of the statements without changing the semantics of the single-threaded procedure.
    2. Reordering of instruction-level parallelism. Modern processors use instruction-level parallelism to overlap multiple instructions. If there is no data dependency, the processor can change the order in which the statement corresponds to the machine instruction execution.
    3. reordering of memory systems. Because the processor uses the cache and read-write buffers, this makes the load and storage operations appear to be executing in a disorderly order.

From the Java source code to the final actual execution of the sequence of instructions, the following three kinds of reordering:

To ensure memory visibility, the Java compiler inserts a memory barrier directive in place of the generated instruction sequence to suppress a particular type of handler reordering. The Java memory model divides the memory barrier into Loadload, Loadstore, Storeload, and Storestore four types:

3.4 Synchronization mechanism

Describes volatile, synchronized, and final

3.5 atomicity, visibility and ordering

Introducing three properties

Excerpt from: http://www.cnblogs.com/nexiyi/p/java_memory_model_and_thread.html

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.