Java as an object-oriented, cross-platform language, its object, memory, etc. has been a difficult point of knowledge. And many of the concepts of the name look so similar, many people will be silly to divide the unclear. For example, the JVM memory structure, the Java memory model, and the Java object model we are going to discuss in this article are three distinct concepts, but many people are easily confused.
It can be said that many advanced developments are not even clear about the concepts and differences between the JVM memory structure, the Java memory model, and the Java object model. I've even met some interviewers who don't know what they're doing. If you do not believe, you go to search the Java memory model on the Internet, there will be a lot of the content of the article is actually introduced the JVM memory structure.
First, these three concepts are completely different from the three concepts. This paper mainly distinguishes these three concepts and introduces them briefly. Each of the knowledge points can be written in a separate article, this article will not be introduced in-depth, interested friends can join my knowledge of the planet and the ball friends to learn together.
JVM Memory Structure
As we all know, the Java code is to run on the virtual machine, and the virtual machine in the process of executing the Java program will be managed by dividing the memory into several different data regions, these areas have their own purpose.
Some of these areas exist as virtual machine processes start, while others depend on the start and end of user threads to build and destroy. The JVM runtime memory area structure is described in the Java Virtual Machine specification (Java SE 8) as follows:
The functionality of each area is not the focus of this article, it is not described in detail here. Here are a few points to pay special attention to:
1, the above is the Java Virtual Machine specification, different virtual machine implementation will be different, but generally adhere to the specification.
2. The method area defined in the specification is just a conceptual area and explains what functions it should have. But it does not specify where the region should be. Therefore, for different virtual machine implementations, there is a certain degree of freedom.
3, different versions of the method area in different locations, the division is the logical region, not the absolute meaning of the physical region. Because the method area in some versions of the JDK is actually implemented in the heap.
4. The running constant pool is used to store various literal and symbolic applications generated during the compilation period. However, the Java language does not require constants to be generated only at compile time. For example, at run time, String.intern will also put new constants into the pool.
5, in addition to the above described JVM runtime memory, there is a memory area to use, that is, direct memory. The Java Virtual Machine specification does not define this area of memory, so he is not managed by the JVM, but is an area of memory that is applied directly out of the heap using the local method library.
6, Heap and stack data division is not absolute, such as the JIT of the hotspot for the object allocation to do the corresponding optimization.
As above, make a summary of the JVM memory structure, defined by the Java Virtual Machine specification. Describes the different data regions that are managed by the JVM during the execution of a Java program. Each region has its own specific functionality.
Java memory model
The Java memory model looks similar to the Java Memory structure (JVM memory structure), and many people mistakenly assume that both are the same thing, which leads to frequent answers during the interview.
In the preceding diagram of the memory structure of the JVM, we can see that the area of the Java heap and the method area is a data region shared by multiple threads. In other words, multiple threads may be able to manipulate the same data that is saved in the heap or in the method area. This is what we often call "Java threads communicating through shared memory."
The Java memory model is translated according to the English Java JMM. In fact, JMM is not as real as the JVM's memory structure. He's just an abstract concept. Described in Jsr-133:java Memory Model and Thread specification, JMM is related to multithreading and describes a set of rules or specifications that define a thread that is visible to another thread when writing to a shared variable.
So, simply summarize, Java multithreading is through shared memory to communicate, and because of the use of shared memory for communication, there will be a series of communication processes such as visibility, atomicity, sequencing, and JMM is around the multi-threaded communication and its related to a series of features built model. JMM defines some syntax sets that map to the Java language, which are keywords such as volatile, synchronized, and so on.
In JMM, we call shared memory for communication between multiple threads called main memory, while in concurrent programming multiple threads maintain a local memory (an abstraction) in which the saved data is a copy of the data in main memory. JMM mainly controls the interaction of data between the local memory and the main memory.
In Java, JMM is a very important concept, it is because of the jmm,java of concurrent programming to avoid many problems. The Java memory model is not described in more detail here, want to know more friends can refer to the "Java Concurrent programming Art".
Java Object Model
Java is an object-oriented language, and Java objects are stored in the JVM with a certain structure. The storage model for the Java object itself is called the Java object model.
In the hotspot virtual machine, a Oop-klass Model is designed. OOP (Ordinary object Pointer) refers to a normal object pointer, and Klass is used to describe the specific type of an object instance.
Each Java class, when loaded by the JVM, creates a instanceklass for the class, stored in the method area, to represent the Java class at the JVM level. When we use new to create an object in Java code, the JVM creates an Instanceoopdesc object that contains the object header and the instance data.
This is the Oop-klass model of a simple Java object, the Java object model.
Summarize
Let's make a distinction between the JVM memory structure, the Java memory model, and the Java object Model three concepts.
The JVM memory structure is related to the runtime region of the Java Virtual machine.
Java memory model, related to concurrency programming in Java.
The Java object model is related to the representation of Java objects in virtual machines.
About these three parts of the content, this article does not separate, because the knowledge points involved is too much, if the reader is interested, can learn by themselves. The following will also be issued to introduce these content, please look forward to.
Finally, these three concepts are very important, must be strictly separated from the open, do not appear in the interview answer not the case.
JVM Memory Architecture vs Java memory model vs Java object model