当前位置:网站首页>jvm-04. Object's memory layout
jvm-04. Object's memory layout
2022-06-23 05:56:00 【CaptainCats】
Memory layout of objects
Object creation
Object o = new Object();
There is a variable in memory o Point to new Out object ,
The blue area represents the object .
Semi initialization problem
adopt new There are three steps to creating objects :
The first step is to apply for memory allocation , This step assigns default values to member variables ,
The second step is to call the constructor , This step assigns the initial value to the member variable ,
Step 3: establish the association between pointer and object .
The first step is to solve c++ The problem of variable legacy values ,
Is the value left by the last program accessing this space , To be safe, set the default value first .
The layout of objects
Common object
It is divided into 4 Parts of :
markword
8 Bytes
class pointer
4 Or 8 Bytes , Pointing to corresponding class
instance data
Object's internal data , For example, member variables
padding
Concatenate the length of the number of bytes so that it can be 8 to be divisible by 
We can use JOL Class library to demonstrate
<!-- https://mvnrepository.com/artifact/org.openjdk.jol/jol-core -->
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.16</version>
<scope>provided</scope>
</dependency>
ObjectLayout
package com.duohoob.jvm.bytecode;
import org.openjdk.jol.info.ClassLayout;
@SuppressWarnings("unused")
public class ObjectLayout {
private static class T {
int a;
int b;
String str = new String("abc");
}
public static void main(String[] args) {
T t = new T();
System.out.println(ClassLayout.parseInstance(t).toPrintable());
}
}
function
com.duohoob.jvm.bytecode.ObjectLayout$T object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x0000000000000001 (non-biasable; age: 0)
8 4 (object header: class) 0xf800c043
12 4 int T.a 0
16 4 int T.b 0
20 4 java.lang.String T.str (object)
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
String Quoting share 4 Bytes , That is to say 32 position ,
Why not 8 Bytes 64 Bit is because the compressed pointer is used ,
How many people do we often talk about , Represents in memory 2 Of n The next address ,
32 The maximum number of bit computers can only use 4G Of memory .
Array
Multiple array lengths 
What does the object header contain
The object header contains markword、class pointer Information ,
markword contain hashCode Information 、synchronized Lock information 、GC Recycling marks .
GC The mark contains three color mark information and generation age .
Object positioning
Direct Pointers
In the heap are instance objects , In the method area is the type class.
Indirect pointer
advantage : Garbage collection doesn't have to be changed frequently t.
shortcoming : slow , You need to locate the pointer first , Then navigate to the object , Two visits .
The allocation of objects
Local variables in functions ,
new You will first try to allocate space within your own stack frame ,
for example for Pass through... In a cycle new Create objects ,
Method execution completed , Stack frame auto Popup , Free up space .
Escape analysis
Analyze the created objects inside the function ,
Is there any other stack frame called besides the current stack frame ,
Whether it has escaped the current stack frame control range ,
If yes, it cannot be allocated on the stack , Can only be allocated on the heap .
边栏推荐
- MySQL面试真题(二十五)——常见的分组比较场景
- MySQL面试真题(二十一)——金融-贷款逾期
- Analysis on the problems and causes of digital transformation of manufacturing industry
- ORB_ Slam2 operation
- What benefits have digital collections enabled the real industry to release?
- PAT 乙等 1017 C语言
- PAT 乙等 1023 组个最小数
- AHA C language Chapter 8 game time is up (lesson 29)
- 三项最高级认证,两项创新技术、两大优秀案例,阿里云亮相云原生产业大会
- Wechat applet: production and generation of love guarantee
猜你喜欢

Wechat applet: Puzzle toolbox

MDM data cleaning function development description

Heimdall database proxy scale out 20 times

Real MySQL interview question (23) -- pinduoduo ball game analysis

The performance of nonstandard sprintf code in different platforms

What does the English letter PC mean? What does the Internet PC mean

jvm-04.对象的内存布局

Android handler memory leak kotlin memory leak handling

数字化工厂建设可划分为三个方面

Huawei's software and hardware ecosystem has taken shape, fundamentally changing the leading position of the United States in the software and hardware system
随机推荐
MySQL面试真题(二十九)——案例-找到爱看的电影
PAT 乙等 1018 C语言
The hierarchyviewer tool cannot find the hierarchyviewer location
Real MySQL interview question (23) -- pinduoduo ball game analysis
visdom的使用
ssm项目搭建
数字藏品市场才刚刚开始
C primer plus学习笔记 —— 2、常量与格式化IO(输入/输出)
Analysis on the problems and causes of digital transformation of manufacturing industry
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-9 personal solutions
android Handler内存泄露 kotlin内存泄露处理
Wechat applet: wechat can also send flash photos to create wechat applet source code download and customize flash time
Real MySQL interview question (30) -- shell real estate order analysis
jvm-06.垃圾回收器
Wechat applet; AI intelligent dubbing assistant
jvm-02.有序性保证
Leetcode topic resolution divide two integers
【数据库备份】通过定时任务完成MySQL数据库的备份
True question of MySQL interview (29) -- case - finding favorite movies
关于安装pip3 install chatterbot报错的问题