Lazy memory initialization for Unsafe (calloc vs. malloc) at at
Mon Nov 5 19:49:47 UTC 2018


lets assume an application A [1] which is optimized for throughput, not
latency. Additionally, A reverts to sun.misc.Unsafe to store gigabytes
of data on off-heap memory to e.g. improved memory utilization and

Due to the fact that Unsafe#allocateMemory returns an uninitialized
(malloc) memory region R, app A has to explicitly initialize R by e.g.
calling Unsafe#setMemory¹.

Since app A is optimized for throughput, there is no real need to
eagerly allocate R. Instead - on OSes with an optimistic memory
allocator such as Linux - app A could benefit from lazy allocation. This
would reduce app startup time that - that today is linear in the size of
R - to constant time. Assuming Unsafe (or some other API) would expose
memory allocation with calloc instead of malloc one would get away
without explicitly calling Unsafe#setMemory or implementing (complex)
lazy initialization into A itself.

To check this idea, I added sun.misc.Unsafe#callocateMemory (notice the
"c") to a local jdk12 build. It works as intended.  Is this feature a
candidate to be added to the JVM, e.g. as part of project Panama or am I
better off rolling my own solution via JNI?


¹ Lets assume the application logic cannot deal with uninitialized memory.


More information about the core-libs-dev mailing list