Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process of creating a virtual machine object by Java

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what is the process of creating virtual machine objects by Java". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the process of creating virtual machine objects by Java" can help you solve the problem.

First, the creation of objects 1.1 new class name

When the virtual machine encounters a new instruction, it first checks whether the parameters of the instruction can locate a symbolic reference to a class in the constant pool, and checks whether the class represented by the symbolic reference has been loaded, parsed, and initialized. If not, perform the corresponding class loading process first.

1.2 allocate memory

The virtual machine allocates memory for new objects. The amount of memory required for an object can be determined after the class is loaded, and allocating memory to an object is equivalent to dividing a determined-sized piece of memory from the Java heap.

(1) there are two ways to allocate memory:

① pointer collision: if the java heap is regular, there is used memory on one side and free memory on the other, with a pointer in the middle as a boundary indicator; to allocate memory, just move the pointer to the idle side to free up space equal to the size of the object.

② free list: if it is irregular, that is, used and free memory are interlaced with each other; the virtual machine needs to maintain a list to record which memory is available; look up the table to find a large enough memory when allocating memory, and update the list record.

The choice of allocation method is determined according to whether the garbage collector used by the virtual machine has the compression function: if the virtual machine of the virtual machine has the compression function, the system uses the pointer collision memory allocation algorithm; otherwise, use the free list algorithm.

(2) Thread safety issues

When concurrently, neither of the above two ways of allocating memory is thread-safe, and there are two solutions:

① synchronous processing

JVM adopts CAS (Compare and Swap) mechanism plus failed retry to ensure the atomicity of the update operation.

CAS: there are three operands, the memory value V, the old expected value A, and the new value B to be modified. If and only if the expected value An is the same as the memory value V, change the memory value V to B, otherwise do nothing

② Local Thread allocation buffer (TLAB)

The act of allocating memory is divided into different spaces according to threads: each thread pre-allocates a small piece of memory in the Java heap, which is called the local thread allocation buffer (Thread Local Allocation Buffer,TLAB); which thread needs to allocate memory from the thread's TLAB; only when the TLAB runs out and needs to allocate a new TLAB, it needs synchronous processing.

JVM specifies whether to use TLAB with "- XX:+/-UseTLAB".

1.3 initialize zero

After the memory is allocated, the virtual machine needs to initialize the allocated memory space to zero. If TLAB is used, it is done when TLAB is allocated. This ensures that the objects (and instance variables) in the program are not explicitly initially assigned zero values, and the program can also access the zero values.

1.4 set object information

The virtual machine sets the necessary information on the object, such as which class the object is, how to find the metadata information of the class, the hash code of the object, the GC generation age of the object and so on. This information is stored in the object's Object Header.

1.5 Construction object

Execute the init method, that is, initialize according to the programmer's wishes. At this point, the objects that are really available are fully constructed.

Memory layout of objects

In HotSpot virtual machines, the layout of objects stored in memory can be divided into three areas: object headers (Header), instance data (InstanceData), and alignment padding (Padding).

2.1 object header

The object header of the HotSpot virtual machine consists of two parts:

(1) the first part is used to store the runtime data of the object itself. The length of this data is 32bit and 64bit in 32-bit and 64-bit virtual machines, respectively. Officially, it is called "Mark Word".

(2) the other part is the type pointer, that is, the pointer of the object to its class metadata, which is used by the virtual machine to determine that the object is an instance of that class.

Not all virtual machine implementations must retain type pointers on the object data, that is, the metadata information of the lookup object does not have to pass through the object itself.

In addition, if the object is a Java array, there must be a piece of data in the object header to record the length of the array, because the virtual machine can determine the size of the Java object through the metadata information of the ordinary Java object, but the size of the array cannot be determined from the metadata of the array.

2.2 instance data

The instance data part is the valid information really stored by the object, and it is also the content of various types of fields defined in the program code. Whether it is inherited from the parent class or defined in a subclass, it needs to be documented. The storage order of this part is affected by the virtual machine allocation policy parameters (FiedsAllocationStyle) and the order in which the fields are defined in the Java source code.

The default allocation policy for HotSpot virtual machines is: longs/doubles, ints, shorts/chars, bytes/booleans, oops (Ordinary Object Pointers). As can be seen from the allocation strategy, fields of the same width are always assigned together. If this prerequisite is met, the variables defined in the parent class will appear before the subclass. If the CompactFieds parameter value is true (the default is true), narrower variables in the subclass may also be inserted into the gaps in the parent variable.

2.3 alignment fill

Alignment padding is not inevitable, nor does it have any special meaning, it only acts as a placeholder. Because HotSpot VM's automatic memory management system requires that the starting address of the object must be an integral multiple of 8 bytes, in other words, the size of the object must be an integral multiple of 8 bytes, and the header part of the object is exactly a multiple of 8 bytes, so when the object instance part is not aligned, it needs to be filled by alignment.

Third, the access location of objects

Objects are created to use objects, and Java programs manipulate specific objects on the heap through reference data on the stack.

The reference type in the Java virtual machine specification provides a reference to the object, but it does not define how this reference should locate the specific location of the object in the access team, so the access mode of the object is also determined by the virtual machine. At present, the mainstream way is to use handle and direct pointer.

3.1 use handle

If accessed as a handle, a block of memory in the Java heap will be divided as a handle pool, and the reference will store the handle address of the object, and the handle contains the specific address information of the object instance data and the type data.

3.2 pointer mode

If you access it as a pointer, then the layout of the Java heap object must consider how to place the relevant information about the access type data, and the address of the object is directly stored in reference. If you only access the object itself, it will reduce the overhead of indirect access.

IV. Comparison of the two ways

The biggest advantage of handle access is that the stable handle address is stored in reference, and only the instance data pointer in the handle will be changed when the object is moved, but reference itself does not need to be modified.

The biggest advantage of pointer access is that it is faster and saves the time overhead of pointer positioning. Because the access to the next part is very frequent in Java, this kind of overhead is also a very considerable implementation cost after adding up.

This is the end of the introduction to "what is the process of creating virtual machine objects by Java". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report