当前位置:网站首页>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 .
边栏推荐
- Lr-link Lianrui makes its debut at the digital Expo with new products - helping the construction of new infrastructure data center
- The e-sports Internet cafe uses a 2.5G network card to experience the feeling of flying!
- ubantu1804 两个opencv版本共存
- A man always becomes a man and a man always arrives (progress, harvest, growth, self-confidence)
- Docker installation redis
- 机器视觉工控机PoE图像采集卡应用解析
- UI automated interview questions
- 银泰百货与淘宝天猫联合打造绿色潮玩展,助力“碳中和”
- Weekly 02 | pour être honnête, je suis un étudiant du MIT
- Chinese text classification based on CNN
猜你喜欢

【数据可视化】Apache Superset 1.2.0教程 (二)——快速入门(可视化王者英雄数据)

第一部分 物理层

JMeter load test finds the maximum number of concurrent users (including step analysis)
![[nk] 牛客练习赛100 C 小红的删数字](/img/f1/a99600e1800c087aceb60a559dee39.png)
[nk] 牛客练习赛100 C 小红的删数字

Application scenario: wide application of Poe network card in NDI technology for live broadcast program production

Add personal statement for go file in file template in Golan

为什么100G网络传输要使用iWARP、RoCE v2、NVMe-oF等协议

解决 img 5px 间距的问题

go语言的goto语句

Part II data link layer
随机推荐
Online excel file parsing and conversion to JSON format
Redis第四话 -- redis高性能原理(多路复用)和高可用分析(备份、主从)
电竞网咖用2.5G网卡,体验飞一般的感觉!
The input value "18-20000hz" is incorrect. The setting information is incomplete. Please select a company
Brain cell membrane equivalent neural network training code
从概率论基础出发推导卡尔曼滤波
How to Load Data from CSV (Data Preparation Part)
[data visualization] Apache superset 1.2.0 tutorial (II) - Quick Start (visualizing King hero data)
Why is your LDO output unstable?
In idea, run the yarn command to show that the file cannot be loaded because running scripts is disabled on this system
Add personal statement for go file in file template in Golan
New product release: domestic single port Gigabit network card is officially mass produced!
Field queryIndexFieldnameService in xxxImpl required a single bean, but 19 were found:
Why should I use iwarp, roce V2, nvme of and other protocols for 100g network transmission
【生活思考】文字与语音
Weekly 02 | pour être honnête, je suis un étudiant du MIT
JVM方法区
MySQL add adds multiple new fields and specifies the field location
Object storage of CEPH distributed storage
Unity screenshot