Depth understanding JVM- reveal the secret of HotSpot object created

Creation of objects

When a virtual opportunity to contain when new instructions will operate a series of objects created:

1. Is there a check mark in the constant pool of the class is about to be created this object belongs to a reference;

a. If no symbol the constant pool of the class reference, illustrate this type have not been defined! Throws ClassNotFoundException;

. b When this class constant pool has a reference symbol, the next step is performed;

2. Further references to check if this symbol represents the JVM class has been loaded;

. A class has not been loaded if, to find class class files into the method area and load;

. B If the class has already been loaded JVM, you are ready to allocate memory for an object;

3. The method of determining region class information in the memory according to the size desired class;

A memory size required for the object can be determined in this object is finished defining your class! And the production of a class of all objects memory size is the same! JVM in the class is loaded into a method to know the time zone for each object class produce the required memory size.

4 corresponds to a division of memory from the heap space to the new object;

Allocated heap memory in two ways:

1) pointer collision

If the JVM's garbage collector uses the copy algorithm or tag - sorting algorithm, then the free memory heap area is full, and between the free memory and a memory used by a pointer flag. Then when allocating memory for an object, the pointer can only move. Thus, this full spare area in memory allocated by way of moving the pointer is called the "pointer collisions."

2) the free list

If the JVM's garbage collector uses mark - sweep algorithm, then the heap free area and the used area staggered, so the need to use a "free list" to record which areas of the heap is a free area, so according to this when creating objects Zhang "free list" to find a free area, and allocate memory.

To sum up: what exactly JVM memory allocation method adopted, depending on its use of what the garbage collector.

The object member variable is assigned the initial value (default initialization);

6. The setting information of the object header;

7. Call the object's constructor initializes

At this point, the whole object of the creation process is complete.

Object memory model

Object from a logical point of view, it consists of member variables and member functions, from a physical point of view, the subject is a string of binary numbers stored in the heap, the organizational structure of this binary sequence in the following.

Objects in the memory divided into three parts:

Object header

Examples of data

Align supplement

1) the object head

Object header record some data objects during operation of the need to use: a hash code, GC generational age, the lock status flags, thread holds the lock, missed the thread ID, timestamp bias.

Further, the object header may also contain a pointer type. You can determine which class the object belongs to by the pointer.

Further, if the object is an array, then the object header also contains the length of the array.

2) Examples of Data

Strength is the value of a member variable data portion, which contains the parent class member variables and member variables of this class.

3) Align supplement

The total length of the object is used to ensure 8 integer multiple of bytes.

The total length of the requested HotSpot object must be an integer multiple of 8 bytes. Since the object header must be an integer multiple of 8 bytes, but the length of the portion of the instance data is arbitrary, and therefore need to be aligned to ensure that the total length of the entire complement target field is an integer multiple of 8.

Process to access an object

We know that reference type variable is stored in an address, then the address depending on the type of object has different access methods:

1) Handle access method

We need to have a heap called the "handle pool" of memory space for storing all types of information objects and addresses of all the object's class.

Reference type variable is stored in the handle of the object address pool. When accessing an object, you first need to find the handle of the object by reference type variable, and then access the object address in the handle of the object.

2) direct pointer access mode

Direct reference type variable storage address of the object, so that no pool handle, direct access to the object by reference.

But additional information like address policy store objects belong to the memory space where the object is required.

Compare

HotSpot direct pointer to access the object because it is only once the addressing operation, so that performance twice as fast as the handle access. But it requires additional policy address class information storage object in the method area.

For more learning materials, you can scan the next Fanger Wei code.

Guess you like

Origin www.cnblogs.com/lemonrel/p/11704572.html