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

How is the underlying layer of java class objects created

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how the bottom layer of java objects is created. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

0. Preface

What exactly does JVM do during the execution of the code of User user = new User (); in the Java program?

1. The process of creating Java class objects

When the Java object is saved in memory, it is mainly composed of three parts: the object header, the instance data, and the alignment padding field, so the process of creating the Java object is actually the process of configuring, supplementing and initializing these three parts.

Note: align padding fields: in JVM, the amount of memory occupied by an object should be a multiple of 8bit. This information is used to complement 8bit and has no other function.

1.1. Class loading check phase

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

1.2. Allocate memory

After the class load check passes, JVM allocates memory for the newly created object. The amount of memory required by the object can be determined after the class is loaded. Therefore, allocating memory space for objects is equivalent to dividing the determined size of memory from the Java heap. There are two allocation methods: "pointer collision" and "free list", which allocation method is determined by whether the Java heap is regular or not, and whether the Java heap is regular or not depends on whether the garbage collector has the compression function (marking-clearly irregular, marking-finishing and copying are all regular).

1. Pointer collision

Application: regular heap memory (no memory fragmentation)

Principle: one side of the used memory and the other side of the unused memory, there is a demarcation pointer in the middle. When allocating memory, you only need to move the pointer to the unused memory direction to determine the memory size and location.

GC Collector: Serial, ParNew

two。 Free list

Application: irregular heap memory

Principle: JVM maintains a list that records which memory blocks are available, finds a large enough memory area to allocate memory to the object instance, and finally updates the memory list

GC collector: CMS

Concurrency problem of memory allocation

Heap memory is shared by threads, so thread safety is an important issue when creating objects to allocate memory. JVM ensures thread safety in two ways.

1. CAS+ failed to retry: CAS is an implementation of optimistic locking. Optimistic locking is to perform the operation without locking each time on the assumption that there is no conflict. JVM uses CAS+ failure to retry to ensure the atomicity of the update operation.

two。 Thread local allocation cache (TLAB): JVM allocates a small area in the Eden area beforehand for each thread-thread local allocation cache (TLAB). When JVM allocates memory to objects in the thread, it first allocates memory in TLAB. When the object is larger than the remaining memory in the TLAB or when the TLAB is exhausted, the JVM will allocate the memory in the CAS+ failure retry mode.

1.3, initialize zero

After the memory allocation is completed, the virtual machine needs to initialize the allocated memory space to zero (excluding the header object). This process ensures that the instance object of Java can be used directly in JVM without assigning initial values, and the program can access the zero values corresponding to the data types of these fields.

1.4. Set the header object

After initializing the zero value, JVM needs to set the necessary object information (the relationship with the class, the metadata information of the associated class, the hash code of the object, the GC generation age of the object), which are stored in the object header of the object.

1.4.1, the form of the header object

There are two ways to object headers in JVM (take 32-bit JVM as an example)

1.4.1.1, ordinary object

| |-|

| | Object Header (64 bits) |

| |-|-|

| | Mark Word (32 bits) | Klass Word (32 bits) | |

| |-|-|

1.4.1.2, array objects

| |-- |

| | Object Header (96 bits) |

| |-- |

| | Mark Word (32bits) | Klass Word (32bits) | array length (32bits) | |

| |-- |

1.4.2, composition of head object

1. Mark Word

two。 A pointer to a class

3. Array length (only array objects)

1.4.2.1 、 Mark Word

Mark Word records information about the object about the lock, and when the object is treated as a synchronous lock by the synchronized keyword, a series of operations around the lock are related to Mark Word.

The length of Mark Word is 32bit in 32-bit JVM and 64bit in 64-bit JVM.

JVM generally uses locks and Mark Word like this:

1, when not treated as a lock, this is an ordinary object. Mark Word records the HashCode of the object, the lock flag bit is 01, and whether the lock bit is biased to the lock bit is 0.

2. When the object is treated as a synchronous lock and a thread A grabs the lock, the lock flag bit is still 01, but whether the lock bit is changed to 1. The previous 23bit records the thread id that grabs the lock, indicating that it has entered the biased lock state.

3. When thread A tries to acquire the lock again, JVM finds that the flag bit of the synchronous lock object is 01, and whether the lock is biased is 1, that is, the biased state. The thread id recorded in Mark Word is thread A's own id, indicating that thread A has acquired the biased lock and can execute the synchronous lock.

4. When thread B tries to acquire the lock, JVM finds that the synchronous lock is biased, but the thread id in Mark Word does not record B, so thread B will first attempt to acquire the lock with the CAS operation. The lock acquisition operation here is likely to succeed, because thread A generally does not automatically release the biased lock. If the lock grab succeeds, change the thread id in Mark Word to thread B's id, which represents thread B to acquire the biased lock and execute the synchronous lock code. If the lock grab fails, proceed to step 5.

5. The failure of grabbing the lock in the biased lock state means that there is some competition in the current lock, and the biased lock will be upgraded to a lightweight lock. JVM opens up a separate space in the thread stack of the current thread to hold a pointer to the object lock Mark Word and a pointer to that space in the object lock Mark Word. The above two save operations are CAS operations. If the save is successful, it means that the thread grabs the synchronization lock, and the lock log bit in Mark Word is changed to 00, and the synchronization lock code can be executed. If the save fails, the lock grab fails, and the competition is too fierce. Proceed to step 6.

6. If the lightweight lock fails to grab the lock, JVM will use the spin lock. The spin lock is not a lock state, but represents a constant retry and attempt to grab the lock. Starting with JDK1.7, spin locks are enabled by default, and the number of spins is determined by JVM. If the preemptive lock is successful, the synchronous lock code is executed, and if it fails, proceed to step 7.

7, if the preemptive lock still fails after the spin lock retry, the synchronization lock will be upgraded to a heavy lock, and the lock flag bit will be changed to 10. In this state, threads that do not grab the lock are blocked.

In 32-bit JVM, Mark Word storage indicates:

The lock flag bit for both unlocked and biased locks is 01, but the previous 1bit distinguishes whether this is unlocked or biased.

Later versions of JDK1.6 have the concept of lock upgrade when dealing with synchronous locks. JVM's processing of synchronous locks starts with biased locks. With the increasingly fierce competition, the processing method is upgraded from biased locks to lightweight locks, and finally to heavyweight locks.

1.4.2.2, pointer to class

The length of the pointer is 32bit in 32-bit JVM and 64bit in 64-bit JVM.

The class data for the Java object is saved in the method area.

1.4.2.3, array length

Only array objects hold this part of the data.

This data is 32bit in length in both 32-bit and 64-bit JVM.

1.5. execute the init method

After the above process is completed, the object instance has been created, but all member variables (property fields) are still zero. So after the new command is executed, you also need to execute the method to initialize the member variables of the class, and the instance object of the class is created.

This is how the bottom layer of java class objects is created. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report