There are three concepts in learning Java: THE JVM memory structure, the Java Memory Model, and the Java Object Model. After our systematic learning, if we do not summarize and compare, it will be easy to confuse, I believe that not only myself sometimes confused, many people should also be confused, so here is a summary and comparison of these three knowledge points. Hope I can help you.



JVM runtime memory structure

During the execution of Java programs, the Java VIRTUAL machine divides the memory it manages into different data areas. Each of these regions has its own purpose and time of creation and destruction. Some regions exist with the start and end of the virtual machine process, while others are created and destroyed depending on the start and end of the user thread.


According to the Java Virtual Machine Specification (Java SE 8), the memory managed by the Java Virtual Machine consists of the following runtime data areas. JVM runtime memory areas are structured as follows:



Java memory model

The variables shared between threads in the Java memory model include instance fields, static fields, and array elements, which are stored in heap memory and shared between threads. Interthread private: Local variables, Format Method parameters, exception Handler Parameters.


Java memory model is mainly to ensure the atomicity, visibility and orderliness of << shared memory part >>, and the memory model defines the specification of multi-threaded program read and write throne behavior in shared memory.

The memory model solves the concurrency problem in two main ways: limiting processor optimization and using memory barriers.


Abstract structure of Java memory model:



Java object Model

Java is an object-oriented language, and the storage of Java objects in the JVM has a certain structure. This storage model of Java objects themselves is called the Java Object Model.


In HotSpot VIRTUAL machine, an OOP-Klass Model is designed. OOP (Ordinary Object Pointer) refers to a Pointer to a common Object, whereas Klass describes the specific type of Object instance. The following figure shows a simple Oop-Klass model of Java objects, or Java Object model.



Four,

By comparison, we can clearly understand the differences between these three concepts:

  • JVM runtime memory structure Java Virtual machine runtime data area
  • Java memory model Java concurrent programming is related to the visibility, atomicity, and orderliness of shared memory.
  • Java object model The storage structure of Java objects. Different objects are stored in different data areas, such as method areas, stacks, and stacks.