当前位置:网站首页>Exploring the depth of objects in JVM series
Exploring the depth of objects in JVM series
2022-06-29 07:09:00 【a_ ittle_ pan】
Start writing
The spirit of the recent period of time is a little depressed , As a result, the first two articles are not satisfactory , So I took a break to update the article .

Reference books :“ In depth understanding of java virtual machine ”
personal java Knowledge Sharing Project gitee Address :Cornucopia
personal java Knowledge Sharing Project github Address :Cornucopia
Memory layout of objects

stay HotSpot In the virtual machine , The memory layout of the object is divided into the following 3 area :
- Object head (Header)
- The instance data (Instance Data)
- Alignment filling (Padding)
The object header contains a type pointer (Class Pointer), By changing the pointer, you can determine which class the object belongs to . If the object is an array object, the object header will also contain the length of the array .
Object head (Header)
stay HostSpot The object header of a virtual machine object contains two types of information :
Used to store runtime data of the object itself , Such as hash code 、GC Generational age 、 Lock status flag 、 A lock held by a thread 、 To the thread ID、 Biased timestamps and so on , The length of this part of data is 32 Bit and 64 The virtual machines of bit are respectively 32bit and 64bit(8byte), It's officially called “Mark World”
The other part of the object header is the type pointer , That is, the pointer of the object to its type metadata (Class Pointer),Java The virtual machine uses this pointer to determine what kind of instance the object is . Besides , Because virtual machines can pass the general Java Object metadata information determination Java Size of object , But if the length of the array is uncertain , The size of the array cannot be inferred from the metadata information . therefore , If the object is an array , There must also be an array in the object header to record the length of the array , It's the one in the picture above Data Length.
The instance data (Instance Data)
Instance data is the real effective information stored by objects , That is, we define various types of field contents in the program code , Whether inherited from the parent class , Fields defined in subclasses must be recorded .
Alignment filling (Padding)
Alignment filling is not inevitable , It doesn't mean anything , It just acts as a placeholder . Why is there such a piece of data because HotSpot The automatic memory management system of virtual machine requires that the starting address of object must be 8byte Integer multiple , In other words, the size of any object must be 8 Integer multiples of bytes . The head of the object has been carefully designed to be exactly 8byte Multiple (1 Times or 2 times ), therefore , If the data part of the object instance is not aligned , It needs to be filled by aligning the padding .
Analyze the case
We already have theoretical knowledge of object memory layout , Now? , Let's use a case to deepen our impression :
Case study :
new user(); Total number of bytes in memory ?
public class user {
private String name;
private int age;
}
analysis
Before we start , We need to create one maven project , Then introduce the JOL tool kit
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.16</version>
</dependency>
Execute code :
public class ObjectTest {
public static void main(String[] args) {
System.out.println(ClassLayout.parseInstance(new user()).toPrintable());
}
static class user {
private String name;
private int age;
}
}
JVM Parameters :
Print the set parameters : -XX:+PrintCommandLineFlags
result :
JVM Parameter printing :
-XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
Terminal output content :
Let's analyze , First of all to see jvm In the parameter XX:+UseCompressedClassPointers , We know that it turns on pointer compression ( That is, it will compress ClassPointer The memory size of , from 8byte->4byte), that , According to the theoretical knowledge we have learned above , An object memory contains three parts , The object header is fixed 8byte,ClassPointer If pointer compression is enabled, it is 4byte,Instance Data here user Contains one int Object occupation 4byte,String Is a reference object that occupies a fixed amount of 4byte, That is to say Instance Data Take up a total of 8byte, because Padding It's a placeholder , Let's not analyze , Here, the object header and object data account for 8+4+8=20, here 20 No 8 Multiple , because jvm Memory management limitations , here jvm Will padding Find distance by 20byte The least 8 To fill in , That is to say 24byte,padding Namely 4byte( That is... In the picture (object alignment gap) Occupied byte size ).
边栏推荐
猜你喜欢

Exclusive download. Alibaba cloud native brings 10+ technical experts to bring new possibilities of cloud native and cloud future

Message queue avoiding repeated refund by idempotent design and atomic lock

Li Kou today's question -324 Swing sort II

How does schedulerx help users solve distributed task scheduling problems?

Analysis on the wave of learning robot education for children

Presto-Trial

NoSQL数据库之Redis(二):Redis配置文件介绍

用机器人教育创造新一代生产和服务工具

package.json的所有配置项及其用法,你都熟悉么

Database - Synonyms
随机推荐
Error: GPG check FAILED Once install MySQL
Chapter V online logic analyzer signaltap
Ci tool Jenkins II: build a simple CI project
What are the conditions for a high-quality public chain?
Ribbon 服务调用与负载均衡
Chapter IV introduction to FPGA development platform
How to fix Error: Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirrorli
Exclusive download. Alibaba cloud native brings 10+ technical experts to bring new possibilities of cloud native and cloud future
二叉树的迭代法前序遍历的两种方法
Better than postman! Apipost knows more about Chinese programmers! How delicious!
Configuring the flutter development environment
软件工程师与软件开发区别? Software Engineer和Software Developer区别?
Message queue avoiding repeated refund by idempotent design and atomic lock
VerilogA——动态比较器
示波器 符号
Summary of some new datasets proposed by cvpr2021
RedisTemplate处理hash整数类型的问题解析
jetson tx2
Suggestions on digital transformation of large chemical enterprises
Redistemplate handles hash integer type problem resolution