当前位置:网站首页>Object creation
Object creation
2022-07-01 19:41:00 【Chenyu】
5、 Object creation
5.1、 Object storage
After the object is created, it will be stored in the heap , And generate a memory address .
stay JVM Objects in are mainly stored in the heap in three parts :
- Object head : Including the layout of heap objects 、 type 、GC The state of 、 Basic information such as synchronization status and identification hash
- Mark Word: Object's own runtime data ( Hash code 、GC Generational age 、 Thread lock information, etc ). stay 32 position JVM The middle length is 32bit, stay 64 Bit JVM Medium is 64bit.
- Type a pointer :JVM Determine the instance of which class the object is created by the type pointer
- The length of the array ( Array objects )
- The instance data : Store the attribute value of the object
- Alignment filling : Align bytes , Populated data , Not necessary . Is to make the fields appear only in the same CPU In the cache line of
5.2、 Object creation process
Programmers write a class
.java The file is compiled as .class file : First, through Javac Compiler will .java To JVM Loadable .class Bytecode file
① Lexical parser : Separate words by spaces 、 The operator 、 Control symbols and other information , formation token information flow , Pass to the grammar parser .
② Syntax parser : hold token Information flows according to Java Grammar rules assemble idiom tree .
③ semantic analyzer : Check whether the keywords are used properly 、 Whether the type matches 、 Whether the scope is correct, etc .
④ Generated bytecode : Convert the information of the previous steps into bytecode .Class loading check : The virtual machine encountered keywords new, First, check whether the parameter corresponding to this instruction can locate the symbolic reference of this class in the constant pool , And check whether the class represented by this symbolic reference is loaded 、 Parsed and initialized . If not, execute class loading .
Class loading : Place the bytecode file in Java In the virtual machine , Load through class loader . There are three modes of execution , Explain to perform 、JIT Compile implementation 、JIT Compiler and interpreter mixed execution ( Main stream JVM Default execution mode ). The advantage of mixed mode is that the interpreter interprets the execution at startup , Save compilation time .
Make sure that after the class is loaded , The virtual machine allocates memory space for objects , The distribution method is Pointer collision 、 Free list Two kinds of .
- Pointer collision : The memory of the heap is regular , The used memory is divided to one side , The free one is on the other side , Use a pointer in the middle as the demarcation indicator , When allocating, move the point to the idle direction by a corresponding distance
- Free list : Memory is not regular , Fragmented , The virtual machine needs to maintain a list to record which memory is available , Find enough space to allocate , And update the list record .
Creating objects requires thread safety , There are usually two ways
- CAS+ Failure to retry : An implementation of optimistic locking , Suppose there is no conflict to create , Retry in case of conflict .
- TLAB: For each thread in Eden To allocate a piece of memory , When allocating memory, first in TLAB Distribute , Object greater than TLAB When the remaining memory or memory is exhausted , Then use CAS.
Initialize zero value : The virtual machine initializes the allocated memory space to zero , Ensure that the object instance field is Java Can be used without assigning initial value .
Set object header : Object is an instance of which class 、 Find different metadata 、 Hash 、GC Age etc.
perform init Method : Initialize an initial object according to the program design
Through the instant compiler JT Compile bytecode files into local machine code .
Java Programs are initially interpreted and executed through interpreters , When the virtual machine finds that a method or block of code is running very frequently , It will be regarded as ” Hot code ”, There are two ways to detect hotspot code, which are based on sampling and counter , In order to improve the execution efficiency of hotspot code , The virtual machine compiles them into local machine code , Optimize the code as much as possible , The back-end compiler that does this at run time is called just in time compiler .
You can also use static precompiler AOT Compile the program directly into binary code related to the target machine instruction set .
边栏推荐
猜你喜欢
精耕渠道共謀發展 福昕攜手偉仕佳傑開展新產品培訓大會
任务:拒绝服务DoS
P2433 【深基1-2】小学数学 N 合一
uni-app商品分类
Solidity - truncated and checked modes of arithmetic operations - new features of 0.8.0
Why has instagram changed from a content sharing platform to a marketing tool? How do independent sellers use this tool?
Instagram 为何从内容共享平台变成营销工具?独立站卖家如何利用该工具?
What is the essential difference between Bi development and report development?
Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
Why must we move from Devops to bizdevops?
随机推荐
How to correctly use vertx to operate redis (3.9.4 with source code analysis)
JS 之 常用内置类的使用
241. Different Ways to Add Parentheses
What is the essential difference between Bi development and report development?
Ffmpeg common commands (2)
【let var const】
703. 数据流中的第 K 大元素
ES6中的代理proxy
ddr4测试-2
[go ~ 0 to 1] day 5 July 1 type alias, custom type, interface, package and initialization function
大厂音视频职位面试题目--今日头条
Live HLS protocol
Methods of finding various limits
H264 encoding profile & level control
Redo和Undo的区别
JDBC中如何添加事务
微信公众号开发相关流程及功能介绍
js三元表达式复杂条件判断
Shell advanced
OpenCV视频质量检测--清晰度检测