Let's consider the question : How does a Java object run in a C + + implementation-based system? How is the object represented inside the JVM? How it is stored in memory ...
1.oop-klass two-part model
Java is an object-oriented language, with three features for object-oriented: encapsulation, inheritance, and polymorphism. And the hotspot is based on C + + implementation, C + + is also an object-oriented language, so that for each Java class generated a C + + class is not OK? This is not the case, the representation of an object within the JVM is designed as a new representation: the Oop-klass binary model:
OOP: or Oops, an ordinary object pointer, used to describe object instance information.
Klass : C + + Peer of the Java class, used to describe the specific type of object instance.
Why design such a model? Cause:The designer of the HotSpot JVM does not want to have a vtable (virtual function table) in each object.
The modules are described below:
Listed above is the architecture of the entire Oops module, which contains multiple submodules, each of which corresponds to a type, and each type of OOP represents a specific object type that is used internally within the JVM. As Java applications run, each Java class object is created, and an OOP object is created inside the JVM to represent the Java object . The common base type of the Oops class is Oopdesc. Depending on the type of object business used within the JVM, there are multiple oopdesc subclasses, each of which represents a specific type of object used within the JVM.
These oops classes have different uses within the JVM:
For example: Instanceoopdesc represents a type instance and Arrayoopdesc represents an array. That is, when we use new to create a Java object instance, the JVM creates a Instanceoopdesc object to represent the Java object, and similarly, when we use the new When you create an instance of a Java array, the JVM creates a Arrayoopdes object to represent the array object.
Inside the JVM, a Java object is represented by a instanceoopdesc. The layout of an object in memory can be divided into two successive parts:instanceoopdesc and instance data . where Instanceoopdesc or Arrayoopdesc is also known as the object header, the object header contains two pieces of information:Mark Word (_mark) and the metadata pointer (_metadata). Where Mark Word stores object runtime record information, into hash code, GC generational age, lock status flag, thread-held lock, biased linear ID, etc. the metadata pointer points to a pointer to the Klass object of the description type, and the Klass object contains the metadata for the type of the instance object. This pointer is used frequently when the virtual runtime is positioned to type information that is located in the method area.
As with Oopdesc, the parent class of other OOP types, Klass is the parent of other Klass types.
Klass provides two functions to the JVM :
Implementation of the Java class at the language level (implemented in the Klass base class)
Implements the distribution of Java objects (provided by the subclass of Klass for virtual function implementations)
the designer of the HotSpot JVM divides the object into Klass and OOP, where the function of OOP is mainly to represent the object's instance data, so it does not contain any virtual functions, and Klass provides a virtual function table in order to implement the virtual function polymorphism. _metadata is a common body, where _klass is a normal pointer, and_compressed_klass is a compression class pointer. Both pointers point to the Instanceklass object, which is used to describe the concrete type of the object .
At run time, the JVM needs a mechanism to identify the type of Java internals. The solution in hotspot is to create a Instanceklass object for each loaded Java class that represents the Java class at the JVM level.
Instanceklass's internal organization:
In the JVM, the basic existence of objects in memory is OOP. Then, the class to which the object belongs is also an object in the JVM, so they are actually organized into an OOP, the klassoop. Similarly, for Klassoop, there is a corresponding Klass to describe, is Klassklass, is also a subclass of Klass. Klassklass as the endpoint of the Klass chain of OOP, such as:
In this design, the JVM allocates and reclaims memory in the same way that it can be managed. OOP can generally be understood as the object we often say, klassoop can be understood as the representation of Java classes in the JVM, and Klassklass describes how to represent a class. In order to represent the storage structure of a Java class in memory more visually, see:
Each Java class, when loaded by the JVM, creates a Instanceklassfor 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 a Instanceoopdesc object that contains two pieces of information: the object header and the metadata. There are some runtime data in the object header, including the lock information related to multithreading, and the metadata maintains a pointer to the instaceklassof the class to which the object belongs.
Java Virtual machine--Object model