The memory model of the Java Virtual machine

Source: Internet
Author: User

First, the origin of the problem

The origin of this article is actually bloggers want to learn java concurrency , the pain point of computer concurrency is that on the one hand CPU more and more nuclear, in addition

One aspect is that the speed gap between CPU and storage and communication subsystem is too large, presumably, the CPU speed is NS, memory 100ns, HDD MS, memory is 100 times times slower than CPU,

Hard disk 1 million times times slower than the CPU, CPU if-and so many of these slow small partners, will inevitably cause the waste of computing resources. Physical machines in order to solve the CPU and memory speed difference,

A cache is added between the two, and a new problem is introduced in the cache, which is the cache consistency issue.

Figure 1 (Image from Reference "1")

In order to solve the cache consistency problem, each CPU needs to follow a certain cache consistency protocol when accessing the cache and main memory.

Abstracting the access to a particular memory and cache is called a " memory model ".

Second, the JVM's memory model

As shown, similar to the structure of CPU access memory in a physical machine, all variables in the JVM memory model are stored in main memory

Each thread has its own working memory (working memories), and the transfer of variables between different threads needs to pass through main memory.

Figure 2 (Image from Reference "1")

As mentioned above, the transfer of variables between threads requires the interaction between working memory and main memory. This interaction follows a certain consistency protocol.

This is simply referred to as the "Interaction Protocol", which is embodied in 8 atomic operations and some operating rules, as follows:

>8 Zhongyuan Sub-operation

    1. Lock: A variable that acts on the main memory and identifies a variable as a thread-exclusive state;
    2. Unlock: A variable acting on the main memory, releasing a variable from the exclusive state of a thread, and releasing the variable before it can be locked by another thread;
    3. READ: A variable that acts on the main memory, transferring a variable from main memory to the working memory of the thread, and this step is only the transfer action;
    4. Load: A variable that acts on the working memory and puts the variable transferred in read into a copy of the working memory;
    5. Use: A variable acting on the working memory, passing a variable in the working memory to the execution engine, that is, the thread is reading the value of the variable;
    6. Assign: A variable that acts on a working memory, assigns a value that is received from the execution engine to the working memory, that is, the value of the thread in the write variable;
    7. Store: A variable that acts on a working memory, transferring the value of a variable to the main memory, which is simply a transfer action;
    8. Write: A variable that acts on the main memory, placing the value of the store's variable in the main memory variable.

> Operating Rules

Not to be continued ...

Reference documents:

"1" in-depth understanding of Java Virtual Machine by Zhou Zhiming

"2" Java concurrent programming actual combat Machinery Industry Press 2012

The memory model of the Java Virtual machine

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.