Java memory model

Source: Internet
Author: User

Several concepts

The Java memory structure, also known as the JVM memory structure, can also be called the data area at run time

Memory model

1, memory structure,

A, Stack

Stacks and threads are closely related. When creating a thread, the Java stack is created, and the Java stack contains multiple frame stacks, and when the method runs, the information exists in the frame stack, such as local variables, return values, operands, and so on. Java stack data is not thread-common, so there is no need to care about its data consistency, and there is no synchronization lock problem

B, Heap

Storing object instances, as well as arrays , garbage collection is mainly for this area, where the data is thread-shared , so it is not thread-safe

C. Method area

Class-related information, such as class member variables, method names, and so on, including, of course, class names, descriptions and other information, the method area also includes a constant pool, storing strings, literals, etc.

D, program counter

Because Java is multithreaded, the thread switches back and forth, and the program counter execution points to the stack of the frame stack to indicate where the program is executing.

E, local method stack

Similar to stacks, except for Java external methods, such as C + + methods.

2. Memory model

The Java Memory Model main program house variable rules, that is, how the variables in the Java Virtual machine are stored in memory, and how to take these details from memory to the virtual machine. JMM Specifies that JMM specifies that all variables are stored in the main memory. Each thread also has its own working memory (working), where the thread's working memory holds a copy of the main memory of the variable used by the thread, and all operations of the thread on the variable (read, assign, and so on) must be made in working memory and not directly read and write to the variables in the main memory . Data exchange must pass through main memory.

The Java memory model is built around the three characteristics of atomicity, visibility, and ordering in concurrent programming.

Visibility, data modification in main memory, working memory is visible. Immediately know.

Ordering, mainly for multi-threaded execution time, some of the threads of the operation of some rules, this is before-happen. For example, volatile modified variables, the write operation occurs first in the read operation,

So the measure of concurrency security must be based on the Happens-before principle.

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.