当前位置:网站首页>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 .
边栏推荐
- Kali modify IP address
- Design and implementation of timer
- Stderr and stdout related standard outputs and other C system APIs
- Source code analysis of synergetics and ntyco
- Yolov4 coco pre train Darknet weight file
- Internal class learning notes
- [paper notes] rethinking and improving relative position encoding for vision transformer
- Summary of four parameter adjustment methods for machine learning
- Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detection
- Two advanced playing methods of QT signal and slot
猜你喜欢

JSON module dictionary and string conversion

Design and implementation of timer

Mining procedure processing

Postman usage notes, interface framework notes

Why do I need message idempotence?

5 connection modes of QT signal slot
![[paper notes] contextual transformer networks for visual recognition](/img/e4/45185983e28664564bbf79023ccbf6.jpg)
[paper notes] contextual transformer networks for visual recognition

Simulating Sir disease transmission model with netlogo

JMeter reading and writing excel requires jxl jar

Image segmentation based on deep learning: network structure design
随机推荐
Business layer - upper and lower computer communication protocol
Learning notes on February 8, 2022 (C language)
Why should the coroutine be set to non blocking IO
2. operator and expression multiple choice questions
Semaphore function
BM setup process
1090.Phone List
Efficient pytorch: how to eliminate training bottlenecks
Statistical analysis - data level description of descriptive statistics
国信金太阳靠谱吗?是否合法?开股票账户安全吗?
The situation and suggestions of a software engineering associate graduate who failed in the postgraduate entrance examination
Js- get the mouse coordinates and follow them
Summary of common methods of ArrayList, LinkedList and vector, and analysis of source code learning
Pytorch | how to save and load pytorch models?
Using R language in jupyter notebook
Data preprocessing - normalization and standardization
Simulating Sir disease transmission model with netlogo
客户经理给的开户链接办理股票开户安全吗?我想开个户
QT source code online view
Go language template text/template error unexpected EOF