当前位置:网站首页>【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类型
边栏推荐
- What are the common proxy servers and what are the differences?
- Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
- Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched
- Connected block template and variants (4 questions in total)
- Email picture attachment
- [reading notes] programmer training manual - practical learning is the most effective (project driven)
- Provincial election + noi Part IV graph theory
- Baohong industry | what misunderstandings should we pay attention to when diversifying investment
- [learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)
- Missing numbers from 0 to n-1 (simple difficulty)
猜你喜欢
使用 useDeferredValue 进行异步渲染
What is the principle of bone conduction earphones and who is suitable for bone conduction earphones
Pychart creates new projects & loads faster & fonts larger & changes appearance
[staff] pitch representation (bass clef | C1 36 note pitch representation | C2 48 note pitch representation | C3 60 note pitch representation)
Ten minutes will take you in-depth understanding of multithreading - multithreaded teamwork: synchronous control
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
2022-2028 global military computer industry research and trend analysis report
[reading notes] programmer training manual - practical learning is the most effective (project driven)
Addition without addition, subtraction, multiplication and division (simple difficulty)
[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
随机推荐
Leetcode face T10 (1-9) array, ByteDance interview sharing
[staff] pitch representation (bass clef | C1 36 note pitch representation | C2 48 note pitch representation | C3 60 note pitch representation)
QT实现界面跳转
[punch in questions] integrated daily 5-question sharing (phase II)
连通块模板及变式(共4题)
[opencv] - comprehensive examples of five image filters
query词权重, 搜索词权重计算
Soul app released the annual report on generation Z behavior: nearly 20% of young people love shopping in the vegetable market
超图iServer rest服务之feature查询
2022-2028 global encryption software industry research and trend analysis report
Vsocde has cli every time it is opened js
Jvm-01 (phased learning)
JVM面试篇
Es interview questions
Remote connection to MySQL under windows and Linux system
結婚後
Leetcode question brushing (10) - sequential question brushing 46 to 50
AcWing 245. Can you answer these questions (line segment tree)
Quality means doing it right when no one is looking
How to turn off the LED light of Rog motherboard