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
- 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:
| Content | Expression |
|---|---|
| Mark Word | Object hashCode or lock information, etc. |
| Class Metadata Address | Object type data pointer |
| Array length | the 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 bit | status | storage content |
|---|---|---|
| 01 | Unlocked | Object HashCode, age of generation |
| 00 | Lightweight lock | Pointer to lock record |
| 10 | heavyweight lock | Pointer to lock record |
| 11 | GC tag | empty |
| 01 | biased | biased 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.
- 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.
- Create object instruction reordering problem
A a = new A();
A simple decomposition of an object:
- Allocate the memory space of the object
- Initialization object
- 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
转载,请保留原文地址,谢谢 ~
What happened when new an object in JVM ?的更多相关文章
- JVM的内存管理机制
在做Java开发的时候常用的JVM内存管理有两种,一种是堆内存,一种是栈内存.堆内存主要用来存储程序在运行时创建或实例化的对象与变量,例如:我们通过new MyClass()创建的类MyClass的对 ...
- (转)走进JVM,浅水也能捉鱼
这不是一篇描述jvm是什么的文章,也不介绍jvm跨平台的特性,也不是讲述jvm安全特性的文章,更不是讲解jvm指令操作,数据运算的文章,本文重点讲述类型的生命周期. 类型的生命周期涉及到:类的装载.j ...
- JVM GC算法
在判断哪些内存需要回收和什么时候回收用到GC 算法,本文主要对GC 算法进行讲解. JVM垃圾判定算法 常见的JVM垃圾判定算法包括:引用技术算法.可达性分析算法. 引用技术算法(Reference ...
- 深入理解JVM虚拟机3:垃圾回收器详解
JVM GC基本原理与GC算法 Java的内存分配与回收全部由JVM垃圾回收进程自动完成.与C语言不同,Java开发者不需要自己编写代码实现垃圾回收.这是Java深受大家欢迎的众多特性之一,能够帮助程 ...
- JVM 专题二十一:垃圾回收(五)垃圾回收器 (二)
3. 回收器 3.1 Serial回收器:串行回收 3.1.1 概述 Serial收集器是最基本.历史最悠久的垃圾收集器了.JDK1.3之前回收新生代唯一的选择. Serial收集器作为Hotspot ...
- JVM七大垃圾回收器下篇G1(Garbage First)
G1回收器:区域化分代式 既然我们已经有了前面几个强大的GC,为什么还要发布Garbage First (G1)GC? 原因就在于应用程序所应对的业务越来越庞大.复杂,用户越来越多,没有GC就不能保 ...
- jvm学习第二天
0.垃圾回收概述 1.什么是垃圾,怎么判断? 1.1引用计数法 含义 顾名思义,此种算法会在每一个对象上记录这个对象被引用的次数,只要有任何一个对象引用了此对象,这个对象的计数器就+1,取消对这个对象 ...
- JVM:Java中的引用
JVM:Java中的引用 本笔记是根据bilibili上 尚硅谷 的课程 Java大厂面试题第二季 而做的笔记 在原来的时候,我们谈到一个类的实例化 Person p = new Person() 在 ...
- Java Knowledge series 3
JVM & Bytecode Abstract & Object Object in Java (1) 所有东西都是对象object.可将对象想象成一种新型变量:它保存着数据,但可要求 ...
随机推荐
- PHP经典面试题目汇总
1.双引号和单引号的区别 双引号解释变量,单引号不解释变量 双引号里插入单引号,其中单引号里如果有变量的话,变量解释 双引号的变量名后面必须要有一个非数字.字母.下划线的特殊字符,或者用{}讲变量括起 ...
- PHP命令空间namespace及use的用法
使用namespace的目的 命名空间将代码划分出不同的空间(区域),每个空间的常量.函数.类(为了偷懒,我下边都将它们称为元素)的名字互不影响, 这个有点类似我们常常提到的'封装'的概念. 团队合作 ...
- 关于flask-sqlalchemy的用法研究
最近使用到flask的sqlalchemy,因为flask对sqlalchemy做了一些封装,加上自己本身对sqlalchemy也不熟悉,用法上走了很多弯路. 因为没时间去研究sqlalchemy的源 ...
- [ch05-00] 多变量线性回归问题
系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 第5章 多入单出的单层神经网络 5.0 多变量线性回归问 ...
- [TimLinux] Python 自定义描述符
1. 含义 在类中,含有属性(该属性需要存在类对象到__dict__属性中,不能为存在示例对象的__dict__属性中),对属性对操作(访问,设置值,删除)可以自定义行为,这样对自定义行为成为自定义属 ...
- 网络安全之Windows基础
1.黑客常用DOS命令 基础: telnet服务:telnet 192.168.1.141 (默认没有打开telnet服务) 常用: color a ping -t -l 65550 ip 死亡之pi ...
- 2020年值得你去试试的10个React开发工具
本文由葡萄城技术团队翻译并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. JavaScript每天都在出现大量的框架和工具,而React是除了上次我们 ...
- iview表单验证trigger:'change,blur'
今天发现,如果设置select的trigger:'blur'就算选择之后还是边框是红色的,之后查了一下iview的文档,也没有找到准确的蚊子描述,只看到form那个组件其中有一个例子,大概是selec ...
- prototype原型
1.prototype是函数的一个属性,并且是函数的原型对象.引用它的必然是函数[对象都是通过函数创建的], 这个prototype的属性值是一个对象(属性的集合,再次强调!),默认的只有一个叫做co ...
- 字符串的扩展(ES6)
文章目录 字符串的扩展 1. 字符的Unicode表示法 2. codePointAt() 3. String.fromCodePoint() 4. 字符串的遍历器接口 5. at()(提案) 6. ...