当前位置:网站首页>Object storage layout, how objects are positioned, how objects are allocated
Object storage layout, how objects are positioned, how objects are allocated
2022-08-06 06:10:00 【helpless coder】
普通对象
普通对象 new Object();任意一个对象,由四块构成.
| 名称 | 解释 |
|---|---|
| markword | 标记字 (ash code、对象年龄、GC标志位、锁状态等信息) |
| class pointer | 类型指针,比如你new T(),Then it points toT.class,用于存放方法区Class对象的地址,The virtual machine uses this address to determine which class the object belongs to. |
| instance data | 实例数据,such as in a classint a = 8;Used to store instance domains,Include the member properties of the parent class |
| padding | 对齐,64Bit binary alignment,相当于8字节对齐.This means that the first three points cannot be added together8整除,Then it needs to be filled,make up for8整除,For example, the first three points add up to 25,Then you need to make up for it8整除,就需要加7位,达到32位.这就是对齐 |
Object memory distribution layout example
引入pom文件
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.9</version>
</dependency>
代码执行
package org.juc.thread;
import org.openjdk.jol.info.ClassLayout;
/** * @ClassName ObjectJOL * @Description Object memory distribution layout * @Date 2022/8/5 9:33 **/
public class ObjectJOL {
public static void main(String[] args) {
Object o = new Object();
//Converted to byte data visible string printing
String s = ClassLayout.parseInstance(o).toPrintable();
System.out.println(s);
}
}
运行结果
标记为1的是markword ,total space8个字节
标记为2的是class pointer,total space4个字节
标记为3is aligned,The front adds up to a total of space12字节,不能被8整除,Four must be added,一共是16个字节,所以newAn object placeholder16字节
案例2
接下里我们new一个对象,Put data in the object,在看一下
package org.juc.thread;
import org.openjdk.jol.info.ClassLayout;
/** * @ClassName ObjectJOL * @Description Object memory distribution layout * @Date 2022/8/5 9:33 **/
public class ObjectJOL {
public static void main(String[] args) {
User test = new User();
test.setName("小明");
test.setAge(8);
String s = ClassLayout.parseInstance(test).toPrintable();
System.out.println(s);
}
static class User{
private String name;
private int age;
public User() {
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
}
运行结果
1. markword 标记位占8个字节
2. class pointer 指向class地址,占位4个字节
3. instance data 实例数据,name和age存放在这里,occupy seats respectively4个字节,一共8个字节
4. padding 对齐,The first three points take up a total of places20字节,不能被8整除,所以需要补位4字节,一共24字节,能够被8整除
对象如何定位?
直接指针:访问速度快
直接定位就是referenceDirectly stored in the heap is the address of the object,If more than just accessing the object itself,Also needing to access the object type data requires one more pointer positioning overhead.
But direct targeting has the benefit of direct targeting,它的速度快,相比于句柄访问(间接定位)It saves the overhead of one positioning,而且java程序运行过程中,Accessing objects is an extremely frequent operation,This omitted addressing is also a very significant implementation cost.HotSpotThe virtual machine uses this method to access objects.
句柄方式,间接指针

The way to indirect pointer objects is through handles,java堆中划分出一块区域作为句柄池,referenceStored in is the address of the handle pool,The handle pool stores the address information of the instance data and type data of the object.There are also advantages to using an indirect way of positioning objects,The heap is the main area where the garbage collector works,Frequent garbage collection operations take place here,There is a lot of object movement involved when doing garbage collection,when moving objects frequently,Just modify the handle's pointer to the instance data,Don't change the stack frequentlyreference变量的值.
对象如何分配?

- First the object will try to allocate on the stack,如果栈分配成功,Then when garbage collection is performed,can be recycled directly,No need for a garbage collector.
- If the object is too large,直接放入Old老年区,只能通过FGC回收.
- 如果对象不大,直接放入Ende区,然后直接GC垃圾回收.在多个线程同时new对象的时候,New objects are stored in Eden区,At this time, there will be competition for resources,So we need to optimize the thread synchronization:我们在EdenA small space is opened up for each thread,默认是1%,It's called thread privateThreadLocalAllocationBuffer ,This way you don't have to synchronize,There will be no competition for resources
Then the new generationEdenThe district will carry out rubbish removalGC,被清除了,就直接删除了,Enter Survivor without clearing S1区,然后经过多次GC,看AGE到了没有,够ageEnter the old age area.
边栏推荐
猜你喜欢
随机推荐
网安大事件丨Fortinet对Apache Log4j漏洞利用的全面复盘与防御
Docker 快速安装&搭建 Mysql 环境
真实力好口碑!Fortinet又双叒叕获评Gartner“客户之选”荣誉称号
Unity Atlas 图集资源依赖打包规则
Unity优化之GC——合理优化Unity的GC
5.3 端口扫描:Zenmap工具的应用
普通工厂类和抽象工厂类的区别
MITRE ATT&CK报告发布 FortiEDR连续两年100%拦截恶意攻击
谷歌浏览器反复提示PageOffice安装
shell之循环语句
Docker 快速安装&搭建 Redis 环境
ARMv7-M Debug Part
Oculus quest2 指南or劝退?
shell之iptables 防火墙
shell之条件测试
Unity3D RectTransform 成员 localPosition 和 anchoredPosition 详解
5.2 主机扫描:主机探测
LVS虚拟服务器中负载均衡玩法
4.3 木马隐藏分析
Redis进阶之一:Jedis、RedisTemplate、StringRedisTemplate之间的比较


![[C语言] 开发系统便利工具](/img/cf/22289f9bd999cb9dcef0c7d81ec3a7.jpg)






