当前位置:网站首页>【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类型
边栏推荐
- 使用开源项目【Banner】实现轮播图效果(带小圆点)
- Es interview questions
- How to create an instance of the control defined in SAP ui5 XML view at runtime?
- how to add one row in the dataframe?
- Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved
- QT使用sqllite
- MongoDB非關系型數據庫
- Pychart creates new projects & loads faster & fonts larger & changes appearance
- Soul app released the annual report on generation Z behavior: nearly 20% of young people love shopping in the vegetable market
- After marriage
猜你喜欢
Actual battle of financial risk control - under Feature Engineering
Jvm-01 (phased learning)
【带你学c带你飞】4day第2章 用C语言编写程序(练习 2.5 生成乘方表与阶乘表
PHP notes - use Smarty to set public pages (include, if, else, variable settings)
Remote connection to MySQL under windows and Linux system
New programmer magazine | Li Penghui talks about open source cloud native message flow system
Cesium dynamic diffusion point effect
【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)
超图iServer rest服务之feature查询
AcWing 245. Can you answer these questions (line segment tree)
随机推荐
2022安全员-C证考试题及模拟考试
Questions d'entrevue
JS slow animation
2022 safety officer-c certificate examination questions and mock examination
結婚後
超图iServer rest服务之feature查询
tarjan2
About DNS
Query word weight, search word weight calculation
【无标题】
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
how to add one row in the dataframe?
Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
[learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
What are the characteristics of common web proxy IP
[JSON] gson use and step on the pit
[pit] how to understand "parameter fishing"
Es interview questions
Face++ realizes face detection in the way of flow
2022-2028 global manual dental cleaning equipment industry research and trend analysis report