[JVM] 6. Gangster small Bai Xuecheng create objects and object positioning access

“JVM small Bai Xuecheng Gangster” series launched until now, received praise from many small partners, also received some suggestions junior partner, to express my gratitude.

There are several small partners raised hopes that describes how to create and access an object, ape Valley has always been a principle of no, the guy who requested what, let’s try to meet, after all, the article is a summary of their own learning and small partners and members opportunity to share learning. Man of few words said, directly open roll up!

1 Create Object

During all the time of running Java programs are object has been created, new methods or reflective or clone or deserialize java objects created may be employed. Next we introduce us in a virtual machine, the object (limited to ordinary Java objects, and does not include an array of Class objects, etc.) of the creation process.

Bytecode representation to create new objects, virtual opportunities to the instruction, the index gain audience in the constant pool, and then navigate to the target object type from the top of the stack. Next, the state of the virtual machine according to the class, take the appropriate memory allocation techniques, examples of allocating space in the memory, and completes the initialization of instance data and the object header. In this way, an object has been created in the JVM.

Create a process instance, according to first get the object type information from the class constant pool and verify that the class has been parsed, if the class has been loaded and ensure proper resolution, using a fast allocation (fast allocation) technology for the class assignment object space ; If the class has not been parsed, the only instance of an object by the slow allocation assignment (slow allocation) method. Create a process instance as shown in FIG.

The basic flow of objects created:

    Verify class has been resolved.

    Get instanceKlass, ensure Klass is fully initialized.

    If the rapid allocation condition is satisfied, then enter the fast allocation process.

    If not meet the rapid distribution conditions, or rapid allocation fails, go to the slow allocation process.

1.1 rapid distribution

If the assignment has been completed before the instance type of resolution, then the distribution operation is merely to divide the available memory space in memory, so memory allocation can be achieved with higher efficiency, which is the rapid distribution.

According to the heap space allocation space from thread private or shared area, rapid distribution space can be divided into two selection strategies. HotSpot dispensing caching (Thread-Local Allocation Buffers, i.e. TLABs) can be achieved by the thread-local space is allocated in the private area of ​​the thread.

You can enable or disable the feature by TLAB VM options UseTLAB.

Depending on whether TLAB, rapid distribution strategies have two options:

    Select TLAB: first try to allocate in TLAB because TLAB is thread-private area, will be able to ensure it does not require locking thread-safe. When allocating a new object space, we will first try to allocate space objects in space TLAB, if requested allocation of space fails, then try to use locking mechanism for allocating objects in the Eden area.

    Select Eden space: if fails, try partitioned region shared Eden, Eden region is shared by all threads regions, the need to ensure thread safety, so the use of atomic operations to be assigned. If the allocation fails, try the operation again until the allocation is successful.

After the instance space allocation succeeds, it will be initialized instance. After completion of the space to be allocated and initialized an object, the object stack can be set by reference. Of course, space allocation and initialization operations are based on the acquisition target object type from the class constant pool and ensure that the premise of the class has been loaded and properly resolved, if the class is not resolved, you need to slow allocation.

1.2 Slow distribution

It has become slow distribution, precisely because of the need to be resolved before the class assignment instance, to ensure that class and dependent class has been correctly interpreted and initialization. Slow allocation module is invoked InterpreterRuntime _new () carried out, codes are as follows.

// 确保要初始化的类不是抽象类型
klass->check_valid_for_instantiation(true, CHECK);
// 确保类已初始化
// 分配实例
oop obj = klass->allocate_instance(CHECK);
// 在线程栈中设置对象引用

Access object positioning 2

To create an object using the object, Java programs needed to operate the specific object on the heap by the reference data on the stack. Because reference types in the Java Virtual Machine Specification specifies a single reference to the object, and this reference is not defined by what means should be to locate the specific location of the object access the heap, so the object access method also depends on the virtual machine implementation and given.

The current mainstream access method uses the handle and have direct pointer in two ways:

  • 如果使用句柄访问的话,那么Java堆中将会划分出一块内存来作为句柄池,reference中存储的就是对象的句柄地址,而句柄中包含了对象实例数据与类型数据各自的具体地址信息,如下图所示。

  • 如果使用直接指针访问,那么Java堆对象的布局中就必须考虑如何放置访问类型数据的相关信息,而reference中存储的直接就是对象地址。即使用直接指针访问在对象被移动时reference本身需要被修改,reference存储的就是对象地址。如下图所示。

Both methods have their advantages Object Access:

    Use the handle to access the greatest benefit is that reference is stable in storage handler address in the instance data object is changed only when you move the pointer in the handle (garbage collection while moving an object very common behavior), while the reference itself does not need modify.

    Direct access manner pointer greatest benefit is 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 a very significant implementation costs .

The second way is to use the case HotSpot object-access, but from the entire range of software development point of view, a variety of languages ​​and frameworks use the handle to access is also very common.

Leave a Reply