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

相关文章

springboot健康检查监控全过程

《springboot健康检查监控全过程》文章介绍了SpringBoot如何使用Actuator和Micrometer进行健康检查和监控,通过配置和自定义健康指示器,开发者可以实时监控应用组件的状态,... 目录1. 引言重要性2. 配置Spring Boot ActuatorSpring Boot Act

使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)

《使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)》在现代软件开发中,处理JSON数据是一项非常常见的任务,无论是从API接口获取数据,还是将数据存储为JSON格式,解析... 目录1. 背景介绍1.1 jsON简介1.2 实际案例2. 准备工作2.1 环境搭建2.1.1 添加

Java实现任务管理器性能网络监控数据的方法详解

《Java实现任务管理器性能网络监控数据的方法详解》在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些... 目录引言一、背景知识二、准备工作1. Maven依赖2. Gradle依赖三、代码实现四、代码详解五

java如何分布式锁实现和选型

《java如何分布式锁实现和选型》文章介绍了分布式锁的重要性以及在分布式系统中常见的问题和需求,它详细阐述了如何使用分布式锁来确保数据的一致性和系统的高可用性,文章还提供了基于数据库、Redis和Zo... 目录引言:分布式锁的重要性与分布式系统中的常见问题和需求分布式锁的重要性分布式系统中常见的问题和需求

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python