Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java nonblocking memory allocation

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.

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

like image 637
Denis Bazhenov Avatar asked Jul 24 '09 11:07

Denis Bazhenov


1 Answers

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).

like image 104
Tnilsson Avatar answered Sep 29 '22 05:09

Tnilsson