JVM chapter of learning - the process of creating an object

This switched: https://www.jianshu.com/p/d2d806ca7859

1. Creating an object

  • Whether the new virtual machine instruction is received, the command checks whether the target symbol in the constant pool of a class reference and a reference to check the class represented by this symbol has been loaded, parsed and initialized. If not, perform class loading process.
  • After the class is loaded by the virtual machine to allocate memory for the new object (a piece to determine the size of the Java heap memory from carved out), memory size can be fully established after the class has finished loading.
  • Two Distribution:
    (1): Pointer Collision: Suppose the Java heap memory is absolutely regular, even if the used memory on one side, the other side of free memory, a pointer to the middle stood as an indicator, by moving the pointer memory allocation.
    (2): free list: If the Java heap memory is not regular, free memory, and memory used already intertwined, the virtual machine must maintain a list of record which memory block is available by from the list Looking for space allocated to the object instance to allocate memory.
  • Whether regular Java heap if used by the garbage collector finishing compression function decision.
  • Creating object is not thread-safe behavior in a virtual machine, it may appear to the object A to allocate memory, not enough time to modify pointer, object B and use the original pointer to allocate memory. There are two solutions:
    (1): The operation of the memory space allocated to sync, in fact, coupled with the virtual machine mode CAS failure retry guarantee atomic update operation;
    (2): in accordance with the memory allocation operation thread divided in different spaces, i.e. each thread a previously allocated heap memory in the Java small, called a local buffer allocated thread (thread Loal allocation buffer, TLAB) .
  • After the completion of memory allocation, it is necessary to allocate memory space values ​​are initialized to zero, to ensure that instances of the object field can not be assigned an initial value in the Java code can be used directly, the type of program can have access to these data fields corresponding to a value of zero .
  • Set the object, the object is an instance of what class, how to find information such as metadata, hash code of the object, GC generational age of the object stored in the object header.

2. The memory layout objects:

  • Objects stored in memory layout can be divided into three: object header (Header), instance data (Instance Data), alignment padding (Padding).
  • Object header, including two pieces of information:
    (1): storing runtime data object itself, such as a hash code, the GC generational age lock state flag thread holds the lock, the thread ID bias, bias timestamp, which the length of the portion of the data in 32-bit and 64-bit virtual machine are 32bit and 64bit, officially called Mark Word (non-fixed data structure, according to the state of the object reuse their own memory space).
     

     

    image

    (2): type of pointer, i.e. a pointer to the class object metadata, the virtual machine is determined by the pointer which is an instance of the object class.
  • Examples of data: real significance information stored in the object, i.e., the various types of program code field contents defined. Whether it is inherited from the father down, or subclass own definition, you have to be recorded.
  • Alignment padding: not necessarily present, plays the role of a placeholder, since HotSpot VM request object size must be an integer multiple of 8 bytes, the head portion and the object is exactly an integer multiple of 8 bytes, so when data is not aligned instance when, by aligning padding to fill full.

3. Access object orientation:

  • Java (local variable table object reference) from the stack and by reference to specific operation objects on the heap, the reference provides only a reference to an object, it does not define how to locate, access to the location of objects in the heap. Object access method implemented by the swift.
    (1): Access Handle: Java heap memory as will be divided into a pool handle, Reference handler address is stored in the object, the handle comprising a respective address information of the object instance data and type data.
     

     

    image

    Advantages: reference stored in the handler address is stable, examples of changing only the data pointer in the handle when the object moves, does not change the reference.
    (2): direct pointers: reference is stored in the object address directly, Java heap place type access target data address (stored in the method area).
     

     

     

    Advantages: faster, saving time cost of a pointer positioning, HotSpot is a direct pointer access.

 

Guess you like

Origin www.cnblogs.com/123-shen/p/11345259.html