当前位置:网站首页>堆内存分配的并发问题
堆内存分配的并发问题
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 + 失败重试的方式分配内存。
边栏推荐
- 专科出身,2年进苏宁,5年跳阿里,论我是怎么快速晋升的?
- 一个女孩子居然做了十年硬件。。。
- Huada 04A operating mode / low power consumption mode
- Want to be a test leader, do you know these 6 skills?
- Publicity of the second batch of shortlisted enterprises! Annual Top100 smart network supplier selection
- Flutter: Unsupported value: false/true
- 干货丨产品的可行性分析要从哪几个方面入手?
- 华大04a工作模式/低功耗模式
- leetcode:515. Find the maximum value in each tree row [brainless BFS]
- Why can some programmers get good offers with average ability?
猜你喜欢
Servlet详解
Main steps of system test
try-with-resources 中的一个坑,注意避让
[200 opencv routines] 209 Color image segmentation in HSV color space
Embedded development: tips and tricks -- clean jump from boot loader to application code
leetcode:55. 跳跃游戏【经典贪心】
Find the maximum value in each tree row [extension of one of the hierarchical traversals]
L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验
Information update on automatic control principle
Ideal L9, new trend of intelligent cockpit
随机推荐
St Table + two points
LINQ query collection class introductory cases Wulin expert class
Development of live broadcast software app, and automatic left-right sliding rotation chart advertising
进程的通信方式
Description of transparent transmission function before master and slave of kt6368a Bluetooth chip, 2.4G frequency hopping automatic connection
Redis-跳表
ansible基本配置
学习笔记23--多传感器信息融合基础理论(上)
How does flutter use the online transcoding tool to convert JSON to model
Information update on automatic control principle
Practice of hierarchical management based on kubesphere
Flutter 如何使用在线转码工具将 JSON 转为 Model
I really can't do it. After 00, I collapsed and wanted to leave
EasyBypass
Drag drag drag
How to refine permissions to buttons?
Disk structure
Machine learning: linear regression
Zero code can apply data visualization to enterprise management
Docker installs MySQL 8.0. Detailed steps