The HotSpot JVM heap objects in

HotSpot is to achieve a JVM, which is currently the most widely applicable scope of the Java Virtual Machine, has the advantage of memory management and accurate style codes hotspot detection technology. The so-called exact Memory Management (Exact Memory Management), refers to the virtual machine can accurately determine the type of data stored in memory, we need to know if you can go further study. HotSpot heap this major recording memory object creation, memory allocation, memory layout and memory access.

First, the process of object creation

We usually use when creating the objects the most is the new keyword, then after the new instructions from, and how to create objects in a virtual machine do?

  • Check parameters : first JVM detects the new instruction, and then check the new instruction parameters (class name) is positioned symbolic references to classes, and checks whether the class has been loaded, parsed and initialized.
  • Allocate memory for the object : a symbol like reference no problem loading has been completed, then begin to allocate memory for the object. It should be noted, in memory after loading the class of each object has been identified, it is divided memory size is known.
  • Memory initialization object : the part of the object instance data in memory all initialized to zero values.
  • Set object : set the object header, storing the necessary information (the object's class, the class metadata pointer, hash code objects, the GC generational age, etc.)
  • At this point the virtual machine is completed, the object is generated in the heap memory. After the virtual machine execution in a Java program <init> method that initializes the object according to the needs of users, after completing the user initialization, the object truly available.

ps: Object creation process has two important parts:

  1. When memory allocation for object heap of different characteristics have different approaches. For regular heap memory (already used memory limit and unused distinct), may allocate memory by pointers collide, the specific implementation is to shift some distance pointer; for irregular heap memory, may be used free list way memory allocation, is about to record the available memory, first of all look at the table when the allocated objects in the free list is large enough memory space, and then distributed.
  2. Heap memory is shared by the threads, concurrency problems may also occur in the memory allocation process. A further object such as memory allocation is finished, B objects are also assigned with the same memory, so that problems occur concurrently. There are two treatment methods, one using CAS + failure retry guarantee the atomicity of memory allocation, two native threads is allocated buffers (Tlab), a small amount of memory assigned to each thread in the heap memory in advance, the thread memory run out on the redistribution of TLAB to it, TLAB distribution needs genlock.

Second, the object memory layout

In the aforementioned memory setting process, and the initialization object, we are provided and the object instance data object header. Then the memory layout of objects divided into three parts:

  • Object header : object object header contains basic information, divided into two parts:
    • Own runtime data: The hash code, generational age lock status. This part of the data length is 32bit / 64bit, determined in accordance with the virtual machine digits.
    • Pointer types: metadata pointer storage class, the class may be determined by the data object belongs.
  • Instance data : instances of data objects is what we often use a variety of fields, which contain inherited fields and define their own fields.
  • Alignment padding : alignment padding just to keep the memory size to ensure that the object is an integer multiple of 8 bytes, no practical significance.

Third, access methods object

After the object is built, we need to access the object by reference variables, operands. The object is divided into two access methods:

  • Handle accessed via pointer access when the handle by the way, we need to open up part of the space in the heap handle as the pool, the pool handle to handle each object stored address (stack) and the type of data (area method) of: Reference data handler address pointing object on the stack, then the type of access to object instances and handle data. The advantage is handler address stored in the stack stabilizing, after the object is collected only need to change the handle, without modifying the stack, the disadvantage is the heap memory space to be divided as part of the pool handle.

 

  • Direct Access : Access direct way is to place the data type information in memory layout object, the reference data directly to objects on the stack. The advantage is direct access to the object faster and use less overhead pointer.

 

Guess you like

Origin www.cnblogs.com/zhengshuangxi/p/11084372.html