当前位置:网站首页>堆内存分配的并发问题
堆内存分配的并发问题
2022-06-24 19:35:00 【Nice2cu_Code】
在实际的开发过程中,会经常的创建对象,作为虚拟机,必须保证线程安全。通常来讲虚拟机采用两种方式保证线程安全
一、CAS + 失败重试
CAS是一种乐观锁的实现方式,每次不加锁假设没有冲突的去完成某项操作,如果因为冲突导致操作失败就重试,直到成功为止。详见博客,传送地址:Java并发编程之 无锁(CAS)
二、TLAB
TLAB是在Java堆空间的伊甸园划分出来的针对每个线程的内存空间,专门在该区域为该线程创建的对象分配内存。
它的主要目的是在并发环境下进行内存分配的时候,减少线程之间对于内存空间的竞争,加速内存分配的速度。
TLAB本质上还是在Java堆中的,所以在TLAB区域的对象,也可以被其他线程访问。
如果没有使用TLAB,多个并发执行的线程创建对象,分配内存的时候,有可能在Java堆中的同一个位置申请,这就需要对这部分内存空间进行加锁或者采用CAS等操作保证线程安全,即保证该区域只分配给一个线程。
使用了TLAB之后,JVM会针对每个线程在堆内存中预留一个内存区域,在预留这个操作发生的时候,需要进行加锁或者采用CAS等操作进行保护,避免多个线程预留同一个区域。一旦确定了某个区域分配给某个线程,之后该线程需要分配内存的时候,会优先在这片区域申请。这个区域对于该线程分配内存这个操作而言是线程私有的,因此在分配的时候不用进行加锁等操作,从而既保护了线程安全又提升了分配速度。
注意: 当该线程创建的对象大于TLAB中的剩余内存或者TLAB的内存已用尽时,再采用CAS + 失败重试的方式分配内存。
边栏推荐
- leetcode:515. Find the maximum value in each tree row [brainless BFS]
- leetcode:55. 跳跃游戏【经典贪心】
- 第二批入围企业公示!年度TOP100智能网联供应商评选
- LINQ query collection class introductory cases Wulin expert class
- Docker 安装 Redis-5.0.12,详细步骤
- In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
- NIO 零拷贝
- 零代码即可将数据可视化应用到企业管理中
- Huada 04A operating mode / low power consumption mode
- Drag drag drag
猜你喜欢

AQS source code analysis

YGG 近期游戏合作伙伴一览

You are using pip version 21.1.2; however, version 22.1.2 is available

Practice of hierarchical management based on kubesphere

NIO、BIO、AIO

一个女孩子居然做了十年硬件。。。
![leetcode:55. Jumping game [classic greed]](/img/da/16e4ab51320d68bd1ade0eb0a2dbc2.png)
leetcode:55. Jumping game [classic greed]

How to automatically remove all . orig files in Mercurial working tree?

Redis+caffeine two-level cache enables smooth access speed

The process from troubleshooting to problem solving: the browser suddenly failed to access the web page, error code: 0x80004005, and the final positioning: "when the computer turns on the hotspot, the
随机推荐
leetcode:55. 跳跃游戏【经典贪心】
Flutter: Unsupported value: false/true
How to refine permissions to buttons?
leetcode:55. Jumping game [classic greed]
The profound meaning of unlimited ecological development in Poka -- Multidimensional Interpretation of parallel chain
Huada 04A operating mode / low power consumption mode
波卡生态发展不设限的奥义——多维解读平行链
NIO 零拷贝
产业互联网时代,并不存在传统意义上的互联网
Yida technology signed a contract with seven wolves to help the digital transformation of "Chinese men's wear leader"
leetcode:515. Find the maximum value in each tree row [brainless BFS]
Minimum spanning tree based on Kruskal
Docker installs redis-5.0.12. Detailed steps
如何提取网页中的日期?
The leader of ERP software in printing industry
NIO、BIO、AIO
Junior college background, 2 years in Suning, 5 years in Ali. How can I get promoted quickly?
NIO、BIO、AIO
KT6368A蓝牙芯片的主从机之前透传功能说明,2.4G跳频自动连接
Description of transparent transmission function before master and slave of kt6368a Bluetooth chip, 2.4G frequency hopping automatic connection