当前位置:网站首页>【JVM】创建对象的流程详解
【JVM】创建对象的流程详解
2022-07-02 02:47:00 【阿小冰】
前言
开发过程中,我们肯定经常会创建一个对象,不要说你不知道,创建对象首先就应该想到new,那你new一下很爽,但是你知道你这么new一下,这个对象怎么就被你创建了呢?这其中的原理你知道吗?不知道没关系,接下来咱们结合图讲解一下
步骤详解
1、JVM收到我们的new指令
首先会先检查一下被new的这个参数是否能在常量池中定位到这个类的符号引用,并且还检查这个符号引用的类是否已经经过了加载、解析、初始化
2、检查当前类是否已被初始化
如果这个类已经被初始化,那就进行下面的序号3的步骤,否则就需要先进行类的加载
3、分配内存
类加载验证通过之后,紧接着就会进行新生对象的内存分配
4、确认内存大小
对象生成需要的内存大小在类加载的时候就可以确定下来了,为对象分配空间等于把一个确定大小的内存从Java堆中划分出来
5、分配的内存大小
主要有两种情况
(1)JVM的内存是规整的,所有使用的内存都放置一边,空闲的内存在另一边,中间放一个指针作为分界点的指示器,这样的话,在分配内存的时候就比较简单了,到时候只需要将指针2指向空闲内存那边挪动一段与对象大小相同的距离即可, 这就是经常说的“指针碰撞”
(2)JVM的内存不规整,相当于是说已使用的内存与未使用的内存彼此交错,这种情况是没办法实现指针碰撞的,那该怎么办呢?只需要维护一张表,来记录那些内存可用的信息即可,在分配的时候从列表中找到一块足够的空间划分给对象实例并更新记录表中的内存状态即可
6、初始化
上面的内存空间分配完之后,JVM需要将内存的空间初始化为0,图中还有移步TLAB,如果使用TLAB,就可以在TLAB分配的时候就可以进行该初始化工作
7、基本信息设置
JVM对对象进行必要的设置,比如哈希码、内存地址等信息
8、生成可执行的对象
在这里需要执行init方法,按照程序中设定的初始化操作来初始化对象,这个时候才算是真正初始化了一个对象,创建了一个虚拟机可执行的java类型
边栏推荐
- [learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
- Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
- Stdref and stdcref
- Query word weight, search word weight calculation
- buu_ re_ crackMe
- As a software testing engineer, will you choose the bank post? Laolao bank test post
- What are the common proxy servers and what are the differences?
- 2022 safety officer-c certificate examination questions and mock examination
- Use the open source project [banner] to achieve the effect of rotating pictures (with dots)
- [learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table
猜你喜欢

Cesium dynamic diffusion point effect

Mongodb base de données non relationnelle

Share the basic knowledge of a common Hongmeng application

Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts

【liuyubobobo-玩转Leetcode算法面试】【00】课程概述

2022-2028 global nano abrasive industry research and trend analysis report

2022-2028 global deep sea generator controller industry research and trend analysis report

The wave of layoffs in big factories continues, but I, who was born in both non undergraduate schools, turned against the wind and entered Alibaba
![[Chongqing Guangdong education] Sichuan University concise university chemistry · material structure part introductory reference materials](/img/ae/7edbdf55795400166650c795c8bd58.jpg)
[Chongqing Guangdong education] Sichuan University concise university chemistry · material structure part introductory reference materials

LFM信号加噪、时频分析、滤波
随机推荐
Infix expression to suffix expression (computer) code
es面试题
Webgpu (I): basic concepts
Set status bar color
Provincial election + noi Part IV graph theory
Which brand of sports headset is better? Bluetooth headset suitable for sports
LeetCode刷题(十)——顺序刷题46至50
Cesium dynamic diffusion point effect
How to batch add background and transition effects to videos?
Après le mariage
Batch detect whether there is CDN in URL - high accuracy
QT uses sqllite
Start from scratch - Web Host - 01
使用 useDeferredValue 进行异步渲染
Kibana操控ES
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
Actual battle of financial risk control - under Feature Engineering
Leetcode question brushing (10) - sequential question brushing 46 to 50
设置状态栏颜色
After marriage
