Chapter 2.HotSpot virtual machine object Quest

Outline

After introducing the runtime data area of ​​the Java virtual machine, let's look at the object is (plain old Java objects only, and does not include an array of Class objects, etc.) how to create? Memory layout object and what kind of it? Java Virtual Machine and how access to this object it

Creating objects

Whether to whether the virtual opportunity when a new instruction, first of all will be to check the parameters of this command can locate a symbol referenced in a class constant pool, and check this symbolic references on behalf of a class has been loaded, parsed and initialized. If not, then you must perform the appropriate class loading process, in Chapter VII will explore in depth the details of this part.

After examination by the class loader, then the virtual machine will newborn object allocation memory. The size of the required memory objects can be completely determined after the class is loaded, the space allocated for the task object is equivalent to determine the size of a piece of memory from the Java heap carved out.

Usually allocate memory in two ways:

  1. Pointer collision: Suppose Java heap memory is absolutely regular, all memory is used aside, free memory are on the other side, the middle stood a pointer as an indicator of the demarcation point, that it is merely to allocate memory moving the pointer size equal to the period of the object distance in the free space there.
  2. Free list: Assuming Java heap memory is not regular, free memory and used memory intertwined, there is no way to simply be a pointer collision, the virtual machine must maintain a list on which memory block is recorded available, to find a large enough space from the list at the time of allocation assigned to the object instance and update records on the list.

Which assignment scheme selection decision on whether Java regular decision, whether regular Java heap and used by the garbage collector with or without compression by the sorting function, therefore, when using Serial, ParNew and other collectors with Compact process, system allocation pointer collision algorithm is used, the use of such CMS-based collector Mark-Sweep algorithm, usually free list.

In addition to how to divide the available space outside, there is another issue to consider is to create objects in a virtual machine is very frequent behavior, even if only to modify the position of a pointer is pointing in the concurrent situation is not thread-safe, possible is not enough time to modify the object a memory allocation, pointer, object B and also assigned to the object a memory address space of memory the same situation.

To solve this problem, there are two solutions:

  1. The operation of the memory space allocated sync - CAS actually coupled by way of a virtual machine failure retry the update operation to ensure atomicity
  2. The memory allocation operation is performed in accordance with different spatial division threads, each thread stack that is a small memory previously allocated in the Java, called a local buffer allocated thread (Thread Local Allocation Buffer, TLAB). Which thread to allocate memory when it allocated on TLAB which thread, only TLAB run out and assign a new TLAB, only need to synchronize locked. Virtual machine using TLAB, by -XX: +/- UseTLAB parameter to decide.

After the completion of memory allocation, virtual machines need to be allocated to the memory space are initialized to zero value (not including the object header), if TLAB, this process can also be carried out work to advance TLAB distribution. This step ensures that the object instance field may not be assigned an initial value in the use of Java code directly, the program can access the data types of zero values ​​corresponding to the fields.

Next, the virtual machine is subject to the necessary settings, such as which object is an instance of the class, how to find metadata confidence class, GC hash code of the object, the object of the sub-generation information age. This information is stored in the object header object among, depending on the current operating status of the virtual machine, such as whether to enable biased lock, head objects have different set up.

This, from the point of view of virtual machines, a new object has been created, but from the point of view Java programs, object creation is just beginning --init method has not executed, all the fields are still zero value. In general (if followed by a bytecode instruction invokespecial determined), after execution of new instructions would then execute the init method to initialize the object in accordance with the wishes of the programmer, this is considered a complete real object produced.

Memory layout objects

In HotSpot virtual machine, the layout object is stored in the memory is divided into three areas: the first objects, instance data, and alignment padding.

Object header

Object header is divided into two parts: an object for storing data and runs its own pointer types

Runtime data storage object itself

Like a hash code, GC generational age, the lock status flags, thread holds the lock, missed the thread ID, timestamp bias. When the pointer is not on compression, which part of the data in 32-bit and 64-bit virtual machine are 32bit and 64bit, officially called "Mark World".

Type pointer

That object is a pointer to its class metadata, the virtual machine is determined by the pointer which is an instance of the object class. But not all virtual machines must retain the data type of the pointer on the object, that is, to find the object metadata information is not necessarily to go through the object itself.

Length of the array

If the object is a Java array, it must also have a data record length of the array used in the object header, since the Java virtual machine can determine the size of the object by the metadata information ordinary Java objects, but not from the metadata of an array to determine the size of the array.

Examples of data

This is truly effective information objects stored.

Align filling

This part is not necessarily exist, there is no special meaning, merely plays the role of a placeholder. Since the HotSpot VM automatic memory management system requires the starting address of the object must be an integer multiple of 8 bytes, that is, the size of the object must be 8 bytes of data. The head portion and the object is exactly a multiple of 8 bytes, because the sample data object is not aligned, it is necessary to completion by aligning padding.

Object Access positioning

Because reference types in the Java Virtual Machine Specification specifies a single reference to the object, and is not defined by this reference should be how to locate, access to objects in the heap of the specific location, so the object access method also depends on the virtual machine achieve.

The current mainstream access method is: use the handle pool and direct pointer.

Handle pool

Java heap will be divided into a memory cell as a handle, the handle is stored in the reference address of the object, and the handle contains the specific address of each object instance data and type data.

Advantages: the object moves, does not require changes in the referene value.

Direct pointer (the HotSpot default)

Direct address is stored in the object reference.

Pros: fast access speed.

Source: Zhou Zhiming of "in-depth understanding of the Java Virtual Machine: JVM advanced features and best practices", Second Edition

Guess you like

Origin www.cnblogs.com/wuqinglong/p/11116214.html
Recommended