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 does a Java object consist of?

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

Share

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

This article focuses on "what is the composition of Java objects", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what Java objects are made of.

When a Java object is saved in memory, it consists of the following three parts:

1, object head

2. Instance data

3, align padding bytes

One, the object head.

The object header of java consists of the following three parts:

1,Mark Word

2, a pointer to the class

3, array length (only array objects)

The object header is divided into three parts:

First, the Mark Word part, you can know from the name that it is a part of recording and describing objects. This is also the main part that I will explain next, because the rest of the content has not changed much. Takes up eight bytes of memory.

Second, the type pointer part, also called metadata pointer and so on, is mainly the address of storing metadata, for the type information of the object, the class information part that points to the method area, for the member variable part of the object, the basic type points to the runtime constant pool of the method area, the String type points to the string constant pool moved from the method area to the heap area after jdk1.7, and other object types point to the object storage address of the heap area. Account for eight bytes of memory, jvm has default on pointer compression, because found that the type pointer part can not use as much as 64 bits, so after being compressed by the pointer, became a four-byte, pointer compression principle, I will not say here, not the focus of this article.

Third, the array data part, specially used to store array data.

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.

Mark Word stores different content in different lock states, as it does in 32-bit JVM:

Lock state

25bit

4bit

1bit

2bit

23bit

2bit

Whether it is biased towards the lock

Lock mark bit

No lock

HashCode of the object

Generational age

0

01

Bias lock

Thread ID

Epoch

Generational age

one

01

Lightweight lock

A pointer to the lock record in the stack

00

Weight lock

A pointer to a weight lock

ten

GC marker

Vbl.

eleven

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.

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.

2, a pointer to the 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.

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.

Second, instance data

The instance data of the object is the properties and their values that can be seen in the java code.

Third, align padding bytes

Because JVM requires that the memory size of the object of java should be a multiple of 8bit, there are a few bytes to make up the size of the object to the multiple of 8bit, with no special function.

At this point, I believe you have a deeper understanding of "what Java objects are made of". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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