What happened when new an object in JVM ?

2024-05-13 00:58
文章标签 java jvm object new happened

本文主要是介绍What happened when new an object in JVM ?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文链接:https://www.javaspring.net/java/what-happened-when-new-an-object-in-jvm

I. Introduction

As you know, Java is an object-oriented programming language. We usually use a variety of objects while writing code. So when you write

User user = new User();

such a line of code, what does the JVM do?

II. Understand the object

  1. Memory Layout

The memory layout of an object in the Hotspot virtual machine is divided into three parts: Object Header, Instance Data, and Alignment Padding.

  • The object header has two parts of information. The first part is used to store the running data of the object itself (HashCode, GC generation age, lock status flag, etc.). The other part is the type pointer, which points to its class metadata. The virtual machine uses this pointer to determine which instance of the class this object is (if there is a handle pool method there is no such thing). If it is an array, there will also be a record array length as shown in the following table:
ContentExpression
Mark WordObject hashCode or lock information, etc.
Class Metadata AddressObject type data pointer
Array lengththe length of the Array

Mark Word is a non-fixed data structure that stores as much information as possible in a very small space, and it multiplexes its own storage space based on the state of the object. The contents of the storage in each state are as follows:

Flag bitstatusstorage content
01UnlockedObject HashCode, age of generation
00Lightweight lockPointer to lock record
10heavyweight lockPointer to lock record
11GC tagempty
01biasedbiased thread ID, biased timestamp, object age

  • The instance data portion is the valid information that is actually stored, that is, the various types of field content defined in the code. Whether it is inherited by the parent class or in the child class.
  • Align padding does not have to exist, it only acts as a placeholder because the HotSpot virtual machine requires that the object's starting address must be an integer multiple of 8 bytes.

2.The object's access

In Java programs we manipulate an object by pointing to a reference to this object. We all know that the object exists in the heap, and this reference exists in the virtual machine stack. So how does the reference locate the location of the objects in the heap?

  • Direct pointer method (HotSpot implementation): The address stored directly in the reference is the address of the object in the heap. The advantage is that the positioning speed is fast, and the disadvantage is that the object movement (the object movement when the GC moves) itself needs to be modified.
  • Handle method: Part of the Java heap is used as a handle pool. The reference stores the handle address of the object, and the handle includes the specific location information of the object instance and type. The advantage is that object movement only changes the instance data pointer in the handle, the disadvantage is two positioning.
  1. The procession of creating an object
  • When the virtual machine encounters a new instruction, it checks whether the parameters of this instruction can locate a symbolic reference to a class in the constant pool and checks whether the represented class has been loaded by the class loader. If it is not loaded then the loading of this class must be performed first.
  • After the class load check is passed, the virtual machine will allocate memory for the new object, and the size of the memory required by the object can be determined after the class is loaded.
  • After the memory allocation is completed, the virtual machine needs to initialize the object to a value of zero, so that the instance variable of the object can be directly used without the initial value in the code. The class variable is initialized to a value of zero during the preparation phase of the class loading.
  • Set the necessary information for the object header, such as how to find the metadata information of the class, the hashCode of the object, the age of the GC, and so on.
  • After the above operation, a new object has been generated, but the method has not been executed, and all fields are zero. At this time, you need to execute the method (construction method) to initialize the object according to the programmer's wishes. The initialization operation of the class variable is completed in the initialization phase of the class loading method.

There are two ways to allocate memory:

  • The Java heap memory is regular (using a markup or a garbage collector with compression), using a pointer to the free location, and allocating memory moves the pointer equal to the allocated size.
  • The memory is not regular (the garbage collector using the markup cleanup), the virtual machine maintains a list of available memory blocks, and when the memory is allocated, a large enough memory space is found from the list to allocate the object and update the available memory list.
  • A GC is triggered when sufficient memory cannot be found

Concurrency problem solution when allocating memory:

Synchronize the actions of allocating memory space---use “the CAS failure retry” to ensure the atomicity of the update operation.Each thread pre-allocates a small amount of memory in the heap, called the Thread Local Allocation Buffer (TLAB), which thread allocates memory on its TLAB, only when the TLAB runs out and allocates a new TLAB. Synchronization lock is required. Set by the -XX: /-UseTLAB parameter.

  1. Create object instruction reordering problem
A a = new A();

A simple decomposition of an object:

  1. Allocate the memory space of the object
  2. Initialization object
  3. Set the reference to the allocated memory address

In the case of 2, 3 and 2 steps, the instruction reordering occurs, which causes problems when accessing the object before initialization in the case of multithreading. The “Double Detection Lock” mode of the singleton mode has this problem. You can use “volatile” to disable instruction reordering to solve problems.

原文链接:https://www.javaspring.net/java/what-happened-when-new-an-object-in-jvm

本文由博客一文多发平台 OpenWrite 发布!

这篇关于What happened when new an object in JVM ?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/984239

相关文章

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2