Java非阻塞内存分配 [英] Java nonblocking memory allocation

查看:47
本文介绍了Java非阻塞内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到某处Java可以在大约12条机器指令中为对象分配内存.这对我来说非常令人印象深刻.据我了解,JVM使用的技巧之一是按块预分配内存.我认为,这有助于最大程度地减少对操作系统的请求数量,这是非常昂贵的.但是,即使是CAS操作,在现代处理器上也可能要花费多达150个周期.

I read somewhere that java can allocate memory for objects in about 12 machine instructions. It's quite impressive for me. As far as I understand one of tricks JVM using is preallocating memory in chunks. This help to minimize number of requests to operating system, which is quite expensive, I guess. But even CAS operations can cost up to 150 cycles on modern processors.

那么,谁能用Java解释内存分配的实际成本以及JVM用来加速分配的技巧?

So, could anyone explain real cost of memory allocation in java and which tricks JVM uses to speed up allocation?

推荐答案

JVM为每个线程(TLA或线程局部区域)预先分配了一个内存区域. 当线程需要分配内存时,它将在该区域内使用碰撞指针分配". (如果空闲指针"指向地址10,并且要分配的对象的大小为50,那么我们只需将空闲指针增大到60,然后告诉线程它可以使用10到59之间的内存作为对象) .

The JVM pre-allocates an area of memory for each thread (TLA or Thread Local Area). When a thread needs to allocate memory, it will use "Bump the pointer allocation" within that area. (If the "free pointer" points to adress 10, and the object to be allocated is size 50, then we just bump the free pointer to 60, and tell the thread that it can use the memory between 10 and 59 for the object).

这篇关于Java非阻塞内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆