当前位置:网站首页>堆内存分配的并发问题
堆内存分配的并发问题
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 + 失败重试的方式分配内存。
边栏推荐
- 波卡生态发展不设限的奥义——多维解读平行链
- Short video mall system, how does scroll view adapt to the remaining height of the page
- Double linked list implementation
- Opengauss kernel: simple query execution
- Information update on automatic control principle
- KT6368A蓝牙双模透传芯片软件版本选型说明
- 直播软件app开发,左右自动滑动的轮播图广告
- 如何提取网页中的日期?
- 985测试工程师被吊打,学历和经验到底谁更重要?
- Want to be a test leader, do you know these 6 skills?
猜你喜欢

Practice of hierarchical management based on kubesphere

Xinlou: Huawei's seven-year building journey of sports health

Redis-跳表

How to compare two or more distributions: a summary of methods from visualization to statistical testing

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

软件设计的七大原则

Cannot find reference 'imread' in 'appears in pycharm__ init__. py‘

Resolving the conflict problem of the flutter Library

leetcode:55. 跳跃游戏【经典贪心】

leetcode:45. 跳跃游戏 II【经典贪心】
随机推荐
St Table + two points
NIO 零拷贝
Summary of papers on traveling salesman problem (TSP)
无心剑汉英双语诗003. 《书海》
In the era of industrial Internet, there is no Internet in the traditional sense
进程的通信方式
Maximum flow problem
socket(1)
Industrial development status of virtual human
树莓派初步使用
Servlet详解
Development trend and path of SaaS industry in China
You are using pip version 21.1.2; however, version 22.1.2 is available
LINQ query collection class introductory cases Wulin expert class
Short video mall system, how does scroll view adapt to the remaining height of the page
Reduce the pip to the specified version (upgrade the PIP through CMP and reduce it to the original version)
AQS source code analysis
Double linked list implementation
学习笔记23--多传感器信息融合基础理论(上)
These map operations in guava have reduced my code by 50%