Java Object Model

Source: Internet
Author: User

Java objects

In memory, a Java object consists of three parts: the object header, the instance data, and the alignment padding. And the object header contains the lock status flag, the thread holds the lock and other flags.

Oop-klass model

OOP (Ordinary object Pointer) refers to a normal object pointer, and Klass is used to describe the specific type of an object instance.

OOP system:

//defines the oops common base classtypedefclassoopdesc*oop;//represents a Java type instancetypedefclassinstanceoopdesc*Instanceoop;//represents a Java methodtypedefclassmethodoopdesc*Methodoop;//represents the invariant information in a Java methodtypedefclassconstmethodoopdesc*Constmethodoop;//data structure for recording performance informationtypedefclassmethoddataoopdesc*Methoddataoop;//defines the abstract base class for array oopstypedefclassarrayoopdesc*Arrayoop;//represents holding an oops arraytypedefclassobjarrayoopdesc*Objarrayoop;//represents an array that holds the base typetypedefclasstypearrayoopdesc*Typearrayoop;//represents the constant pool described in the class filetypedefclassconstantpooloopdesc*Constantpooloop;//Chang tell the cachetypedefclassconstantpoolcacheoopdesc*Constantpoolcacheoop;//describes a C + + class that is equivalent to a Java classtypedefclassklassoopdesc*Klassoop;//represents the object headertypedefclassmarkoopdesc* Markoop;

as shown in the preceding code, the Oops module contains multiple submodules, each of which corresponds to a type, and each type of OOP represents a type of specific object used within the JVM. One of the variants of the OOP type Oopdesc is the common base type of the Oops module. The Oopdesc type also contains subclass types such as Instanceoopdesc (class instance),Arrayoopdesc (array), and so on. The Instanceoopdesc contains the following parts of the data:markoop _mark and Union _metadata , and some different types of field.

As the Java program runs, each time a new Java object is created, a corresponding type of OOP object is created within the JVM to represent the Java object. In the hotspot virtual machine, the object contains three areas in memory: Object header, instance data, and alignment padding. The object header contains two parts:_mark and _metadata, while the instance data is stored in the various field defined in Oopdesc.

_mark:

_mark This section is used to store the runtime data of the object itself, such as hash code, GC generational age, lock status flag, thread-held lock, bias thread ID, bias timestamp, and so on, the length of the data in 32-bit and 64-bit virtual machines (no compression pointers are turned on) 32bit and 64bit, the official call it "Mark Word". Objects need to store a lot of runtime data, in fact, beyond the 32-bit and 64-bit bitmap structure can record the limit, but the object header information is independent of the object's own defined data, additional storage costs, considering the virtual machine space efficiency, Mark Word is designed to be a non-fixed data structure that stores as much information as possible in a very small amount of space, which re-uses its own storage space based on the state of the object.

_metadata:

_metadata This part is a type pointer, a pointer to its class metadata, which the virtual machine uses to determine which class the object is an instance of. Not all virtual machine implementations must keep type pointers on object data, in other words, finding the object's metadata information does not necessarily go through the object itself, depending on how the virtual machine implements the object access. At present, the main way to access the use of a handle and the direct pointer two, the difference is not introduced here first.  also , if the object is a Java array, then there must be a piece of data in the object header to record the length of the array, because the virtual machine can determine the size of the Java object through the metadata information of the ordinary Java object, but the size of the array cannot be determined from the array's metadata.

Klass

Klass system:

//a part of the klassoop used to describe the type of language layerclassKlass;//describe a Java class at the virtual machine levelclassInstanceklass;//proprietary Instantklass, representing Java.lang.Class's KlassclassInstancemirrorklass;//A proprietary instantklass that represents the Klass of a java.lang.ref.Reference subclassclassInstancerefklass;//represents the Klass of MethodoopclassMethodklass;//represents the Klass of ConstmethodoopclassConstmethodklass;//represents the Klass of MethoddataoopclassMethoddataklass;//as the end of the Klass chain, Klassklass's Klass is its ownclassKlassklass;//represents the Klass of InstanceklassclassInstanceklassklass;//represents the Klass of ArrayklassclassArrayklassklass;//represents the Klass of ObjarrayklassclassObjarrayklassklass;//represents the Klass of TypearrayklassclassTypearrayklassklass;//represents an abstract base class for an array typeclassArrayklass;//represents the Klass of ObjarrayoopclassObjarrayklass;//represents the Klass of TypearrayoopclassTypearrayklass;//represents the Klass of ConstantpooloopclassConstantpoolklass;//represents the Klass of ConstantpoolcacheoopclassConstantpoolcacheklass;

As with Oopdesc, the parent class of other OOP types, the Klass class is the parent of other Klass types.

Klass provides two functions to the JVM:

    • Implementing a language-level Java class (already 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 has designed a oop-klass model that divides objects into Klass and OOP, because they do not want each object to contain a virtual function table. Where oop is used primarily to represent the object's instance data, so there is no virtual function. In order to realize the virtual function polymorphism, Klass provides a virtual function table. So, on the Java polymorphism, there are actually C + + virtual functions of the shadow.

Instanceclass

At run time, the JVM needs a mechanism to identify the type of Java internals. The solution in hotspot is to create a Instanceclass object for each loaded Java class that represents the Java class at the JVM level.

INSTANCECLASS Internal structure:

//List of methods owned by the classObjarrayoop _methods; //Describe method orderTypearrayoop _method_ordering; //the implemented interfaceObjarrayoop _local_interfaces; //inherited InterfacesObjarrayoop _transitive_interfaces; //DomainTypearrayoop _fields; //ConstantsConstantpooloop _constants; //class LoaderOOP _class_loader; //protected DomainOOP _protection_domain; ....

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, it is klassklass, is also a subclass of Klass. Klassklass as the endpoint of the Klass chain of OOP, its Klass is itself.

Memory storage

Let's start with a look at the storage structure of the code below.

classmodel{ Public Static intA = 1;  Public intb;  PublicModel (intb) { This. B =b; }} Public Static voidMain (string[] args) {intc = 10; Model Modela=NewModel (2); Model Modelb=NewModel (3);}

The storage structure is as follows:

From this we can conclude that an instance of the object (INSTANTOOPDESC) is stored on the heap, and the object's metadata (instantklass) is stored in the method area, and the object's reference is saved on the stack.

Summary

When the JVM loads the Java class, the JVM creates a instanceklass for the class and saves it in the method area, which is used to represent the Java class at the JVM level. When we create an object using the New keyword, the JVM creates an Instanceoopdesc object that contains two pieces of information, object header and meta data. There are some runtime data in the object header, which includes information about the locks associated with multithreading. Metadata maintains a pointer to the instanceklass of the class to which the object belongs.

Resources

In-depth understanding of multithreading (ii)--java object model-hollischuang ' s Blog

In-depth understanding of multithreading (iii)--java object header-hollischuang ' s Blog

Java Object Model

Related Article

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.