当前位置:网站首页>Object creation process of JVM
Object creation process of JVM
2022-06-11 21:05:00 【Just number six Z】
JVM Object creation process of
Java Object creation process ( Five steps )
1. Class loading check
virtual machine Meet a new When the command , First, we will check whether the parameters of this instruction can locate the symbol reference of this class in the constant pool , And check whether the class represented by this symbol reference has been loaded 、 Parsed and initialized . without , The corresponding class loading process must be performed first .
2. Allocate memory
After the class load check , The virtual machine is new The object that comes out allocates memory .
The memory size required for the new object is determined after the class is loaded , What we need to do now is to convert a certain size of memory from Java Divide it up in the heap .
There are two ways of distribution : Pointer collision and Free list .
If the memory space is regular
If memory is regular , So the virtual machine will use Pointer collision method (Bump The Pointer) To allocate memory for objects . It means that all used memory is on one side , Free memory on one side , There is a pointer in the middle as an indicator of the dividing point , Allocating memory is just an example of moving the pointer to the free side for a period equal to the size of the object . If the garbage collector chooses Serial、ParNew This compression algorithm is based on , Virtual machines are distributed in this way , Generally used with compact( Arrangement ) Process collector , Use pointer collisions .
If the memory is out of order
If memory is not regular , Used and unused memory interlace with each other , Then the virtual machine uses the space-time free list method to allocate memory for the object . The virtual machine maintains a list , Record the memory blocks that can be used , When reallocating, find a large enough space in the list to divide it into the formation instance , And update the list . This distribution becomes “ Free list (Free List)”.
Select which allocation method is determined by Java Whether the pile is regular or not , and Java Whether the heap is regular or not depends on whether the garbage collector adopted has compression and collation function .
Add : Memory allocation concurrency problem
There is a very important problem when creating objects , It's thread safety , In actual development , Creating objects is a frequent thing , As a virtual machine , You have to be thread safe .
CAS + Failure to retry
CAS It's an implementation of optimistic lock . Do not lock each time, but assume that there is no conflict to complete an operation , If the conflict fails, try again , Until we succeed .
Virtual machine adopts CAS The atomicity of update operation is guaranteed by the way of failure retry .
TLAB
In advance for each thread Eden Allocate a piece of memory ,JVM When allocating memory to objects in a thread , First, in the TLAB Distribute , When the object is greater than TLAB Memory remaining in or TLAB When I ran out of memory , Then use the above CAS Distribute .
3. Initialize zero value
After the memory allocation is complete , The virtual machine needs to initialize the allocated memory space to zero ( Object headers are not included ), This step ensures that the instance field of the object is in the Java Code can be used directly without initial value , The program can access the zero value corresponding to the data type of these fields .
4. Set object header
The virtual machine should make necessary settings for the objects , For example, this object is an instance of that class 、 How can I find the metadata information for a class 、 Object's hash code 、 Object's GC Information such as generational age . This information is stored in the object header . in addition , Depending on the current running state of the virtual machine , Such as whether to enable biased lock, etc , Object headers can be set differently .
5. perform init Method
stay Java From the perspective of procedure , Initialization just started . Initialize member variables , Execute instantiation code block , Call the constructor of the class , The first address of the object in the heap is assigned to the reference variable .
边栏推荐
- [data visualization] Apache superset 1.2.0 tutorial (III) - detailed explanation of chart functions
- Go语言for循环
- Role of RESNET residual block
- gateway先启动其他微服务,在启动网关,网关启动不了,且无异常日志;先启动网关,所有服务能正常启动
- Serval and Rooted Tree(CF1153D)-DP
- [game theory complete information static game] strategic game
- Black circle display implementation
- How to add text on the border in bar code software
- Field queryIndexFieldnameService in xxxImpl required a single bean, but 19 were found:
- What are striplines and microstrip lines? Reference planes and transmission lines
猜你喜欢
![[nk] 牛客练习赛100 C 小红的删数字](/img/f1/a99600e1800c087aceb60a559dee39.png)
[nk] 牛客练习赛100 C 小红的删数字

PHP strtotime 获取自然月误差问题解决方案

Yintai department store and Taobao tmall jointly create a green fashion show to help "carbon neutrality"

Teach you how to use win7 system to quickly build your own website

应用场景:现场直播节目制作NDI技术中PoE网卡的广泛应用

Redis fourth session - redis high performance principle (multiplexing) and high availability analysis (backup, master-slave)

What are striplines and microstrip lines? Reference planes and transmission lines

新品发布:LR-LINK联瑞推出首款25G OCP 3.0 网卡

php pcntl_ Fork create multiple child process resolution

Three waves of changes in cloud computing
随机推荐
为什么100G网络传输要使用iWARP、RoCE v2、NVMe-oF等协议
Systematically study the recommendation system from a global perspective to improve competitiveness in actual combat (Chapter 8)
BCC tool tool usage
Serval and Rooted Tree(CF1153D)-DP
Online excel file parsing and conversion to JSON format
In idea, run the yarn command to show that the file cannot be loaded because running scripts is disabled on this system
Go language for loop
Toolbar替换ActionBar后Title不显示
How to Load Data from CSV (Data Preparation Part)
Serval and Rooted Tree(CF1153D)-DP
Modify appid of local wechat applet
Title does not display after toolbar replaces actionbar
全球机器视觉市场规模持续上涨,PoE图像采集卡为工业相机提供高速传输通道
MySQL add adds multiple new fields and specifies the field location
[data visualization] Apache superset 1.2.0 tutorial (II) - Quick Start (visualizing King hero data)
Docker installing MySQL
Object storage of CEPH distributed storage
第一部分 物理层
Summary of C language programming knowledge points 01
Yintai department store and Taobao tmall jointly create a green fashion show to help "carbon neutrality"