"In-depth understanding of the Java virtual machine" - Exploration of the Object

Load class:

Java virtual machine specification, there is no obligation, when to begin loading, however, it has strict rules several cases

必须进行初始化(加载,验证,准备则需要在初始化之前开始)

  • Encounter new, getstatic, putstatic, 4 or invokestatic bytecode instructions that, if no class not been initialized, initialization is triggered.
  • Use java.lang.reflect package approach, reflecting confront when called, if not initialized, the first trigger initialization
  • A class initialization time, if we find the parent class is not initialized, the first trigger initialization of the parent class.


From the class is loaded into memory to start the virtual machine until unloading a far memory, its entire life cycle, including:

Load (Loading), verification (Verification), preparation (Preparation), analytical (Resolution), initializing (Initialization), using (Using) and unloading (Unloading) 7 stages. Wherein, verification, preparation and parsing collectively connected (Linking). Process is shown below:


load

Loading phase will do three things:

  • To obtain such a binary byte stream defined by the fully qualified name of a class.
  • This represents the byte stream of static storage structure into a run-time data structure area method.
  • Generating a representative of the Java heap java.lang.Class object of this class, such as a method of access to the data area entry.

Relative to other stages of class loading, loading phase (specifically, the operation is a binary stream of bytes acquired class loading phase) is controllable strongest stage, because the developer can use the system to provide both class loader is to finish loading, you can also customize your own class loader to finish loading.

After loading phase is completed, the external storage format of virtual machines on a binary stream of bytes required for virtual machines in accordance with the method area, but also create a class java.lang.Class objects in the Java heap, so that we can through the these data access objects area.

verification

Verify whether the data type JVM specification, whether it is a valid byte code file to verify that covers the validation class data format, semantic analysis, verification operation.

ready

Assigned to the class static variable memory, and initialize the default values, which memory is allocated in the method area, you need to pay attention to the following points:

  • Herein comprises variable memory allocation only class variables (static), and does not include the instance variables, instance variables is allocated in the heap with the java object instantiation.
  • Here the default value is the default data type (e.g., 0,0L, null, false), instead of the value given code is displayed.
  • If ConstatntValue property class field attribute table present field, i.e. while being modified final and static, it will be initialized to the value specified in the attribute ConstValue preparation stage variable value.

Resolve

The resolution phase is the constant pool of the virtual machine process is replaced symbolic references direct reference, analysis operation for the main qualifier class or interface, fields, methods class, interface method, type method, and calls the method handle 7 points for the class reference symbols. Symbolic reference is a set of symbols to describe the target can be any literal.

Direct reference is the direct pointer to the object, an indirect or offset relative to the positioning target handle.

initialization

Initialize be the last phase of class loading process, at this stage there is the real start java代码主导. The code is in a class all of the static keyword identifies the uniform implementation of it again, if you are performing a static variable, it will use the initial value set in the preparation phase before the user-specified value coverage; if you are performing static代码块,那么在初始化阶段,JVM就会执行static代码块中定义的所有操作.


Objects created:

Create Object (clone deserialization) is typically only a newkeyword, whereas in the virtual machine, for example, the step of creating an object: 

① When the virtual opportunities to the new instruction. The first parameter to check whether this command in the constant pool of a class targeted to a reference symbol, and a reference to check whether the class represented by this symbol is loaded, parsed and initialized. Assuming no. That must perform appropriate 类载入过程 .


② class loading later in check. Next freshmen will target virtual machine memory allocation. Object required memory size determined after class loading. For the task object is equivalent to the allocation of space 一块确定大小的内存从Java堆划分出来 .

  ②.① the process of creating an object is actually a non-thread-safe procedure, it is also need to consider thread safety issues. A possible target is to allocate memory, not enough time to change pointer, the object B and the same time using a pointer to the original allocation of memory. To solve this problem is: 

  A scheme, the operation of the memory space allocated sync - CAS actually coupled by way of a virtual machine failure retry ensure atomic update operation.

  Scheme II, the memory allocation operation is performed in accordance with different spatial division threads. That is, each thread a small piece of pre-allocated heap memory in Java. Known as a local thread allocation cache (TLAB). Which thread to allocate memory when it allocated on TLAB which thread, just had TLAB run out and assign a new TLAB, only need to sync lock. Virtual machine using TLAB, through -XX: +/- UseTLAB to set the parameters.


After ③ memory allocated. Virtual machine memory space assigned to both 初始化为零值(不包括对象头), assuming TLAB, this process can work to advance when TLAB distribution, this step ensures that the object instance in the field Java代码中能够不赋初始值就能直接使用,程序能訪问到这些字段的数据类型所相应的零值.


④ Next Virtual confidential Dui 对象进行必要的设置, such as: what this object is an instance of the class, how to find the caliber metadata information class, the hash code of the object, the object GC generational age of information. This information is stored in the header information of the object being. According to different execution state of the virtual machine. As whether biased lock, the object will have different head arrangement. 


After completion of the above work, 从虚拟机角度来看,一个新的对象已经产生了, but the Java program from the point of view, an object has only just begun - (init) method has not been executed. All fields are also zero, so, in general.执行new命令后。会接着执行init方法。把对象依照程序猿的意愿进行初始化,这样一个真正可用的对象才算全然产生出来。 


Memory layout objects:

In HotSpot VM, the object is stored in memory layout may be divided into three areas:

  • Object header (Header)
  • Examples of data (Instance Data)
  • Alignment padding (the Padding)

Object head HotSpot virtual machine consists of two pieces of information:

A first portion for storing runtime data object itself, such as a hash code (HashCode), GC generational age lock state flag thread holds the lock, the thread ID bias, bias timestamp, this data part is called Mark Word.  

Another part of the object header pointer type, i.e., a pointer to its class object is metadata, the virtual machine is determined by the pointer which is an instance of the object class.

Examples of data: significance information object is actually stored is defined in the program code field of various types of content.

Filling alignment: alignment padding is not necessarily exist, there is no special meaning, it only plays a role in the placeholder.


Access Object location:

When a Java program using an object needs to manipulate objects on the heap by specific reference data on the stack.

The current mainstream way to have access to use 句柄and 直接指针两种.

  • If then the handle access, 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 information of each data type data object instances. as the picture shows:


  • If direct access pointer, then the Java heap layout object must be considered the type of access information of how to place the data, it is directly stored in the target address reference, as shown:



Guess you like

Origin juejin.im/post/5d5511475188257761189dca