当前位置:网站首页>【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类型
边栏推荐
- MySQL operates the database through the CMD command line, and the image cannot be found during the real machine debugging of fluent
- STM32__05—PWM控制直流电机
- [staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
- [staff] restore mark (Introduction to the use of restore mark | example analysis of Metaphone mark and restore mark)
- C write TXT file
- Es interview questions
- Deployment practice and problem solving of dash application development environment based on jupyter Lab
- About DNS
- query词权重, 搜索词权重计算
- PHP notes - use Smarty to set public pages (include, if, else, variable settings)
猜你喜欢

批量检测url是否存在cdn—高准确率

Soul app released the annual report on generation Z behavior: nearly 20% of young people love shopping in the vegetable market

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

连通块模板及变式(共4题)

【带你学c带你飞】3day第2章 用C语言编写程序(练习 2.3 计算分段函数)

How to hide the scroll bar of scroll view in uniapp

CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强

Baohong industry | four basic knowledge necessary for personal finance

Multi threaded query, double efficiency
![[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the](/img/fe/d97b25f702bbc05f941d08147259e0.jpg)
[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the
随机推荐
MongoDB非關系型數據庫
Realize the code scanning function of a custom layout
[learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)
buu_ re_ crackMe
QT使用sqllite
Mongodb non relational database
2022-2028 global wood vacuum coating machine industry research and trend analysis report
QT实现界面跳转
LeetCode刷题(十)——顺序刷题46至50
STM32__ 05 - PWM controlled DC motor
Unit · elementary C # learning notes
After marriage
If you want to rewind the video picture, what simple methods can you use?
What are the characteristics of common web proxy IP
2022-2028 global soft capsule manufacturing machine industry research and trend analysis report
Connected block template and variants (4 questions in total)
2022低压电工考试题模拟考试题库模拟考试平台操作
Calculation (computer) code of suffix expression
【OpenCV】-5种图像滤波的综合示例
研发中台拆分过程的一些心得总结
