当前位置:网站首页>Brief object memory layout
Brief object memory layout
2022-06-25 15:32:00 【ITenderL】
Object memory layout
HotSpot In the virtual machine , The layout of objects stored in memory can be divided into three areas : Object head (Header)、 The instance data (Instance Data) And align fill (Padding).
1. Object head
Java The object header of an object consists of three parts :
- Mark Word
- Type a pointer
- The length of the array ( Only array objects have )
1.1 Mark Word
MarkWord Used to store runtime data of the object itself , Such as hash code (HashCode)、GC Generational age 、 Lock information, etc .
Mark Word stay 32 position JVM The length in is 32bit, stay 64 position JVM The middle length is 64bit.
For example, in 32 Bit HotSpot A virtual machine in which objects are not locked ,Mark Word Of 32 individual bits In the space 25bits Used to store object hash codes (HashCode),4bits Used to store object generational ages ,2bits Used to store lock flag bits ,1bit Fixed for 0, In other states ( Lightweight locking 、 Heavyweight lock 、GC Mark 、 Can be biased ) The storage contents of the following objects are shown in the following table .

1.2 Type a pointer
The pointer is in 32 position JVM The length in is 32bit, stay 64 position JVM The middle length is 64bit.
The type pointer points to the class metadata of the object , The virtual machine USES this pointer to determine which class the object is an instance of .
Java The class data of the object is stored in the method area .
1.3 The length of the array
If the object is an array , Then the object header needs extra space to store the length of the array .
The length of this data also increases with JVM Architecture is different :32 Bit JVM On , The length is 32 position ;64 position JVM Then for 64 position .
64 position JVM If open +UseCompressedOops Options , The length of the area will also be determined by 64 Bit compression to 32 position .
2. The instance data
Object instance data is in java The attributes you can see in the code and their values .
3. Align fill bytes
because JVM requirement Java The amount of memory occupied by the object should be 8bit Multiple , So there are a few bytes after that to make up the size of the object to 8bit Multiple , No special function .
4. Use JOL To analyze java Object layout
JOL The full name is Java Object Layout. It's an analysis of JVM in Object Layout gadgets . Include Object Occupancy in memory , Instance object reference, etc .
JOL You can use... In your code , It can also be run independently from the command line . I will not introduce the command line in detail here , Today, I will mainly explain how to use in code JOL.
Use JOL Need to add maven rely on :
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.9</version>
</dependency>
adopt JOL see jvm Information :
public class Test01 {
public static void main(String[] args) {
Object o = new Object();
System.out.println(ClassLayout.parseInstance(o).toPrintable());
}
}
Output :

First explain the meaning of each field
OFFSETIt's the offset , That is, the space occupied by the location of this field byte Count ,SIZEIs the size of the latter type ,TYPEyes Class Type defined in ,DESCRIPTIONIs a description of the type ,VALUEyesTYPEValue in memory .
As can be seen from the above figure Object o = new Object(); In memory 16 Bytes .
边栏推荐
- Learning C language today is the first time to learn C language. In college, C linguistics is not good, but I want to make progress, so I found a beep video on the Internet to learn C language
- Principle and implementation of MySQL master-slave replication (docker Implementation)
- Common operations in VIM
- Source code analysis of synergetics and ntyco
- Completabilefuture of asynchronous tools for concurrent programming
- Go language modifies / removes multiple line breaks in strings
- System Verilog — interface
- Why should the coroutine be set to non blocking IO
- Pytorch | how to save and load pytorch models?
- How to package rpm
猜你喜欢

Could not connect to redis at 127.0.0.1:6379 in Windows

1090.Phone List

Using R language in jupyter notebook

Yolov5 Lite: fewer parameters, higher accuracy and faster detection speed
![[C language] implementation of magic square array (the most complete)](/img/b2/2595263b77e0abac667972bbfe0c8a.jpg)
[C language] implementation of magic square array (the most complete)

Source code analysis of synergetics and ntyco

Globally unique key generation strategy - implementation principle of the sender

Summary of regularization methods

Several common optimization methods

basic_ String mind map
随机推荐
到底要不要去外包公司?这篇带你全面了解外包那些坑!
Go build reports an error missing go sum entry for module providing package ... to add:
[C language] implementation of magic square array (the most complete)
Source code analysis of zeromq lockless queue
Leetcode122 timing of buying and selling stocks II
3. Sequential structure multiple choice questions
Basic syntax and common commands of R language
Statistical analysis - data level description of descriptive statistics
Software packaging and deployment
Using reentrantlock and synchronized to implement blocking queue
[paper notes] rethinking and improving relative position encoding for vision transformer
MySQL field truncation principle and source code analysis
Pytorch | how to save and load pytorch models?
剑指 Offer 06. 从尾到头打印链表
Boost listening port server
Sampling method and descriptive statistical function in R language
Common dynamic memory errors
(1) Introduction
Es data synchronization mode
JMeter reading and writing excel requires jxl jar