当前位置:网站首页>堆內存分配的並發問題
堆內存分配的並發問題
2022-06-24 22:27:00 【Nice2cu_Code】
在實際的開發過程中,會經常的創建對象,作為虛擬機,必須保證線程安全。通常來講虛擬機采用兩種方式保證線程安全
一、CAS + 失敗重試
CAS是一種樂觀鎖的實現方式,每次不加鎖假設沒有沖突的去完成某項操作,如果因為沖突導致操作失敗就重試,直到成功為止。詳見博客,傳送地址:Java並發編程之 無鎖(CAS)
二、TLAB
TLAB是在Java堆空間的伊甸園劃分出來的針對每個線程的內存空間,專門在該區域為該線程創建的對象分配內存。
它的主要目的是在並發環境下進行內存分配的時候,减少線程之間對於內存空間的競爭,加速內存分配的速度。
TLAB本質上還是在Java堆中的,所以在TLAB區域的對象,也可以被其他線程訪問。
如果沒有使用TLAB,多個並發執行的線程創建對象,分配內存的時候,有可能在Java堆中的同一個比特置申請,這就需要對這部分內存空間進行加鎖或者采用CAS等操作保證線程安全,即保證該區域只分配給一個線程。
使用了TLAB之後,JVM會針對每個線程在堆內存中預留一個內存區域,在預留這個操作發生的時候,需要進行加鎖或者采用CAS等操作進行保護,避免多個線程預留同一個區域。一旦確定了某個區域分配給某個線程,之後該線程需要分配內存的時候,會優先在這片區域申請。這個區域對於該線程分配內存這個操作而言是線程私有的,因此在分配的時候不用進行加鎖等操作,從而既保護了線程安全又提昇了分配速度。
注意: 當該線程創建的對象大於TLAB中的剩餘內存或者TLAB的內存已用盡時,再采用CAS + 失敗重試的方式分配內存。
边栏推荐
- Notes on writing questions (18) -- binary tree: common ancestor problem
- Yida technology signed a contract with seven wolves to help the digital transformation of "Chinese men's wear leader"
- Zero code can apply data visualization to enterprise management
- Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
- NIO、BIO、AIO
- Redis+caffeine two-level cache enables smooth access speed
- LINQ query collection class introductory cases Wulin expert class
- How does flutter use the online transcoding tool to convert JSON to model
- Embedded development: tips and tricks -- clean jump from boot loader to application code
- Resolving the conflict problem of the flutter Library
猜你喜欢
The logic of "Ali health" has long changed
NiO, bio, AIO
Heartless sword Chinese English bilingual poem 003 The sea of books
try-with-resources 中的一个坑,注意避让
How to refine permissions to buttons?
I really can't do it. After 00, I collapsed and wanted to leave
leetcode:55. Jumping game [classic greed]
[200 opencv routines] 209 Color image segmentation in HSV color space
“阿里健康”们的逻辑早就变了
leetcode:55. 跳跃游戏【经典贪心】
随机推荐
Machine learning: gradient descent method
interrupt、interrupted 、isInterrupted 区别
Huada 4a0gpio settings
TCP RTT measurement tips
Information update on automatic control principle
first-order-model实现照片动起来(附工具代码) | 机器学习
Process communication mode
是真干不过00后,给我卷的崩溃,想离职了...
KT6368A蓝牙芯片的主从机之前透传功能说明,2.4G跳频自动连接
Learning notes 23-- basic theory of multi-sensor information fusion (Part I)
Detailed explanation of agency mode
L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验
How does flutter use the online transcoding tool to convert JSON to model
String exercise summary 2
Drag drag drag
华大4A0GPIO设置
零代码即可将数据可视化应用到企业管理中
Seven principles of software design
LINQ query collection class introductory cases Wulin expert class
“阿里健康”们的逻辑早就变了