Distinguish JVM memory structure, Java memory model and Java object model three concepts

This paper consists of Jane Wyatt SimpRead transcoding, the original address https://www.toutiao.com/i6732361325244056072/

作者:Hollis 
来源:公众号Hollis 

Java as an object-oriented, cross-platform language, its objects and memory has been more difficult knowledge points. And a lot of names and concepts seem so similar, many people will innocently could not tell. For example, this article we will discuss the JVM memory architecture , Java memory model and Java object model , which is three distinct concepts, but many people tend to confuse.

It can be said that many senior developers do not even know JVM memory structure, the difference between the concept of Java memory model and Java object model and between these three. I have seen some even engage the interviewer himself is not too clear. Do not believe it, you go online to search for Java memory model, there will be a lot of content articles is actually introduced is JVM memory structure.

First of all, these three concepts are completely different three concepts. In this paper, a distinction between these three concepts as well as a brief introduction. Each of which knowledge can write a separate article, I will not go in-depth reports, interested friends can join my knowledge planet golfers and learn together.

JVM memory structure

We all know that, Java code to run on a virtual machine, the virtual machine and the memory division during the execution of the Java program will manage for a number of different data regions, which have their own purposes.

Some of the region with the launch of the virtual machine process exists, and some regions are dependent on start and end users to create and destroy threads while. In the "Java Virtual Machine Specification (Java SE 8)" described in the following memory area structure JVM runtime:

This article focuses on the function of each area is not, not here described in detail. Here a brief mention a few points need special attention:

1, these are the Java Virtual Machine Specification, different virtual machine implementations will vary, but will generally comply with specifications.

2, method area defined in the specification, except the area on a concept and described what function it should have. But it does not specify where it should be in this area in the end. So, for different virtual machines, it is a certain degree of freedom.

3, different location area different versions of the method, the image above is divided logical area, a physical area is not in an absolute sense. Because some versions of the JDK method in the heap area is actually implemented.

4, runtime constant pool for storing various applications literal symbols and compile generated. However, Java language does not require constant can only be generated at compile time. For example, at runtime, String.intern will be the new constant into the pool.

5, in addition to the above described JVM runtime memory outside, there is an area of ​​memory available, that is, direct memory. Java Virtual Machine specification does not define this memory area, so he does not manage the JVM is the use of native method libraries apply directly outside the heap memory area.

6, heap and stack data partitioning is not absolute, such as the HotSpot JIT will do the appropriate optimized for object allocation.

As above, to be a summary, JVM memory structure, defined by the Java Virtual Machine Specification. It describes the process of executing a Java program, different data area managed by the JVM. Each region has its own specific function.

Java Memory Model

Java Java memory model looks and memory structures (JVM memory structure) similar, many people will mistakenly think is the same thing children, which also led to the interview process as often as non-A.

In Figure on JVM memory structure in front, we can see that the area where the Java heap and method area is shared by multiple threads of data area. That is, a plurality of operating threads may be the same data stored in the stack area or the method. This is what we often say "between Java threads communicate via shared memory."

Java memory model is based on English Java Memory Model (JMM) translated. In fact, JMM is not as JVM memory structure is the same as real. He was just an abstract concept. JSR-133: Java Memory Model and Thread Specification describes, JMM is multi-threaded and relevant, he described a set of rules or specification that defines when a thread writes to shared variables of another thread is visible of.

So, the simple conclusion is between Java multithreading communication via shared memory, while the result of shared memory communication, there will be a series of problems such as visibility, atom, sequence, etc. in the communication process, the JMM model is built around a multi-threaded communications, and a series of associated properties. JMM defines the set of grammar, the syntax is projected into the Java language is volatile, synchronized and other keywords.

In JMM, we shared memory communication between multiple threads called main memory, and multiple threads in concurrent programming maintains an own local memory (this is abstraction), in which data is stored in main memory the data copy. JMM and mainly to control the data exchange between the local memory and the main memory.

In Java, JMM is a very important concept, it is precisely because JMM, Java concurrent programming to avoid a lot of problems. Java memory model here is not to do a more detailed introduction, I would like to know more friends can refer to the "art Java concurrent programming."

Java object model

Java is an object-oriented language, Java objects stored in the JVM is a certain structure. And this on its own Java object storage model called the Java object model.

HotSpot virtual machine, designed an OOP-Klass Model. OOP (Ordinary Object Pointer) refers to a common object pointer, and for the particular type described Klass object instance.

每一个 Java 类,在被 JVM 加载的时候,JVM 会给这个类创建一个 instanceKlass,保存在方法区,用来在 JVM 层表示该 Java 类。当我们在 Java 代码中,使用 new 创建一个对象的时候,JVM 会创建一个 instanceOopDesc 对象,这个对象中包含了对象头以及实例数据。

这就是一个简单的 Java 对象的 OOP-Klass 模型,即 Java 对象模型。

总结

我们再来区分下 JVM 内存结构、 Java 内存模型 以及 Java 对象模型 三个概念。

JVM 内存结构,和 Java 虚拟机的运行时区域有关。

Java 内存模型,和 Java 的并发编程有关。

Java 对象模型,和 Java 对象在虚拟机中的表现形式有关。

关于这三部分内容,本文并未分别展开,因为涉及到的知识点实在太多,如果读者感兴趣,可以自行学习。后面也会发文介绍这些内容,敬请期待。

最后,这三个概念非常重要,一定要严格区分开,千万不要在面试中出现答非所为的情况。

Guess you like

Origin www.cnblogs.com/wangdaijun/p/11461255.html