当前位置:网站首页>In depth study of JVM bottom layer (II): hotspot virtual machine object
In depth study of JVM bottom layer (II): hotspot virtual machine object
2022-07-02 06:48:00 【I think starfish_ ninety-eight】
List of articles
HotSpot Virtual machine objects
Object creation
Main process :
Determine whether the class corresponding to the object is loaded 、 link 、 initialization : The virtual machine received a new When the command , First, I will check whether the specified parameter can be in Constant pool To locate a symbolic reference to a class , And check the class represented by this symbol reference Whether it has been loaded by the class loader 、 Linked and initialized . If not, execute the corresponding class loading process first .
Allocate memory for objects : Choose which allocation method is by Java Pile up Whether it is regular or not Of , and Java Whether the heap is regular or not depends on whether the garbage collector is equipped with compression and sorting function
- Pointer collision : If Java The memory of the heap is regular , That is, all the used memory is put aside , And the free ones are on the other side . When allocating memory, move the pointer indicator in the middle to the free memory by a distance equal to the size of the object , This completes the allocation of memory .
- Free list : If Java Heap memory is not regular , The virtual machine needs to maintain a list to record what memory is available , In this way, when allocating, you can query the list to allocate enough memory to the object , And update the list record after allocation .
Dealing with concurrency security issues
Synchronize actions to allocate memory space ( use CAS + Failed retries to guarantee atomicity of update operation );
The action of memory allocation is divided into different spaces according to threads , That is, each thread Java A small chunk of memory is pre-allocated in the heap , Called local thread allocation buffer (Thread Local Allocation Buffer, TLAB). Which thread will allocate memory , On which thread TLAB The distribution of . Only TLAB Use up and assign new TLAB when , Need synchronization lock . adopt -XX:+/-UserTLAB Parameter to set whether the virtual machine uses TLAB.
Initialize the allocated memory space : After memory allocation , The virtual machine should initialize the allocated memory space to zero ( Object headers are not included ). If used TLAB, This step will arrive ahead of time TLAB On assignment . This step ensures that the instance fields of the object are in Java Code can be used directly without initial value .
Set the object header of the object : Set object header (Object Header) Information , Include the class of the object 、 Object's HashCode And objects GC Data such as generation age is stored in the object header of the object .
perform init Method to initialize : perform init Method , Initialize the member variables of the object 、 Call the constructor of the class , So an object is created
Memory layout of objects
HotSpot
In the virtual machine , The layout of objects stored in memory can be divided into three areas : Object head (Header
)、 The instance data (Instance Data
) and Alignment filling (Padding
).
Object head
stay HotSpot In the virtual machine , The object header consists of two parts of information : Runtime data and Type a pointer , If it's an array object , There is also a space to hold the length of the array .
- Mark Word( Runtime data ): Used to store the data of the object's own runtime , Such as hash code (hashCode)、GC Sub band age 、 A lock held by a thread 、 To the thread ID Etc . stay 32 Bit system occupies 4 byte , stay 64 In the bit system 8 byte ;
- Class Pointer( Type a pointer ): Used to point to the corresponding object Class object ( Its corresponding metadata object ) Memory address of . stay 32 Bit system occupies 4 byte , stay 64 In the bit system 8 byte ;
- Length: If it's an array object , There is also a space to hold the length of the array , Occupy 4 Bytes
The instance data
The instance data It's the object that actually stores Effective information , Whether from Parent inheritance It's still this kind of thing Self , It needs to be recorded , And this part of Storage order By the virtual machine Allocation policy and The order of definition Influence .
The allocation policy is always in the order of byte size from large to small , Put the same byte size together .
Alignment filling
No special meaning , It's not necessary , Placeholder only .
HotSpot
The virtual machine requires that each object be Initial address Must be 8
Integer multiples of bytes , It has to be the size of the object 8
Bytes of An integral multiple . and Object header Is precisely 8
Multiple of bytes (32
Position as 1
times ,64
Position as 2
times ), therefore , When the object instance data part is not aligned , It needs to pass Alignment filling To make up for .
Object access location
Java
The program needs to go through JVM
References on the stack access specific objects in the heap . How objects are accessed depends on JVM
The realization of virtual machine . At present, the mainstream access methods are Handle and Direct Pointers Two ways
Handle access
Java
The heap is divided into a block of memory as Handle to the pool , Of the object stored in the reference Handle address , And the handle contains Object instance data And Object type data Respective Specific address Information , The specific structure is shown in the figure below :
advantage : What is stored in the reference is Stable Handle address of , When the object is moved ( Moving objects during garbage collection is a very common behavior ) It just changes In handle Of Instance data pointer , and quote There is no need to modify itself .
Direct Pointers
If you use Direct Pointers visit , quote The direct storage in is Object address , that Java
The layout inside the heap object must consider how to place access Type data Information about .
advantage : Speed faster , Saved Once the pointer is positioned Time cost of . Because the access of the object Java
Very often , As a result, this kind of overhead can add up to a considerable execution cost .HotSpot That's how it works .
边栏推荐
- Idea announced a new default UI, which is too refreshing (including the application link)
- 2020-9-23 use of QT timer qtimer class.
- js判断对象是否为空
- Uploading attachments using Win32 in Web Automation
- Eslint configuration code auto format
- 20201002 vs 2019 qt5.14 developed program packaging
- MySQL index
- Latex compiles Chinese in vscode and solves the problem of using Chinese path
- 解决微信小程序swiper组件bindchange事件抖动问题
- Vector types and variables built in CUDA
猜你喜欢
table 组件指定列合并行方法
Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
How to debug wechat built-in browser applications (enterprise number, official account, subscription number)
pytest(1) 用例收集规则
The default Google browser cannot open the link (clicking the hyperlink does not respond)
Uploading attachments using Win32 in Web Automation
ctf三计
Win电脑截图黑屏解决办法
Latex compiles Chinese in vscode and solves the problem of using Chinese path
随机推荐
VSCODE 安装LATEX环境,参数配置,常见问题解决
Selenium memo: selenium\webdriver\remote\remote_ connection. Py:374: resourcewarning: unclosed < XXXX > solution
2020-9-23 QT的定时器Qtimer类的使用。
20201002 vs 2019 qt5.14 developed program packaging
Kali latest update Guide
selenium备忘录:selenium\webdriver\remote\remote_connection.py:374: ResourceWarning: unclosed<xxxx>解决办法
Usage of map and foreach in JS
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
web自动化切换窗口时报错“list“ object is not callable
默认google浏览器打不开链接(点击超链接没有反应)
CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
ModuleNotFoundError: No module named ‘jieba.analyse‘; ‘jieba‘ is not a package
The default Google browser cannot open the link (clicking the hyperlink does not respond)
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
No process runs when querying GPU, but the video memory is occupied
virtualenv和pipenv安装
pytest(1) 用例收集规则
Fe - eggjs combined with typeorm cannot connect to the database
JS modification element attribute flipping commonly used in selenium's Web Automation