java interview - object creation, memory layout, access the location

First, create an object

1, when the virtual opportunity to a new directive, first to check whether the parameters of this command to locate a symbol referenced in the class constant pool, and checks whether the class represented by this symbolic reference has been loaded, parsed and initialized. If not, then you must perform the appropriate class loading process.

2, the virtual machine will allocate an object of newborn

  • Pointer collision: the Java heap memory is absolutely regular , all memory is used aside, free memory on the other side, the middle stood a pointer as an indicator cut-off point, and that the allocated memory is just the moving the pointer size equal to the period of the object distance in the free space there

  • Free list: Java heap memory is not regular, free memory and used memory intertwined, the virtual machine must maintain a list of which memory blocks are available on the record, find a list from the time allocated plenty of space allocated to the object instance, and update records on the list.

3, the virtual machine needs to allocate memory space is initialized to a value of zero (not including the object header)

4, the virtual machine is subject to the necessary settings, for example, which this object is an instance of the class, how to find information such as metadata, hash code of the object, GC target sub-generation information age. This information is stored in the object object head (Object Header) in

5, the implementation of <init> method, in accordance with the wishes of the programmer to initialize the object, so that a real object is considered completely available emerge.

Second, the object memory layout

1, object header (Header)
  • Mark Word: storage of runtime data object itself, such as the hash code (HashCode), GC generational age, the lock status flags, thread holds the lock, missed the thread ID, timestamp bias .

        This part of the length of data in 32-bit and 64-bit virtual machine (not open compressed pointer), respectively, and for the 32bit 64bit, officially called "Mark Word".

  • Type pointers: a pointer to its class object is metadata, the virtual machine is determined by the pointer which this object is an instance of the class
Examples of classes of
2, instance data (Instance Data): real significance information stored in the object,
Various types of field contents in the program code as defined herein. Whether it is inherited from the father down, or defined in a subclass, you need to record them.
 
3, alignment padding (Padding): need not be present, only for byte alignment.

Third, access the location of the object

java program required to operate the particular object on the heap by the reference data on the stack.

Handle visit:

Java heap will be divided into a memory cell as a handle , reference is stored in the handle of the object address , and the handle contains the address information of each specific object instance data and the data type.
Benefits: reference is stable in storage handler address will only change the instance data pointer in the handle when the object is moved (moving object when garbage collection is a very common behavior), while the reference itself does not need to be modified.

Direct pointers:

Java heap layout object must be considered relevant information on how to access the data type of placement, and direct reference is stored in the object address.
Benefits: faster, it saves time overhead of a pointer is positioned due to access objects in Java very frequently, so after such expenses add up is also a very substantial implementation costs.

 

Guess you like

Origin www.cnblogs.com/wjh123/p/11408658.html