JNI faster than Unsafe?

Robert Stupp snazy at snazy.de
Sat Feb 7 09:02:13 UTC 2015

Hi there,

I've setup a single microbenchmark just to know which off-heap memory allocation is faster on Linux + OSX. Candidates were Unsafe.allocateMemory and JNA's Native.malloc(). Both either against "raw" OS and against a preloaded jemalloc (LD_PRELOAD).

My initial suspicion was, that both (Unsafe vs. Native) should perform quite equally since both methods are basically just wrappers for C malloc(). But they don't.

Native.malloc() is much faster compared to Unsafe.allocateMemory().
Depending on the individual microbenchmark, Native.malloc() is up to 3 times faster than Unsafe.allocateMemory().

Here's a spreadsheet, that I've worked out during the benchmark.
(you may safely ignore the jemalloc-via-JNA stuff)

The JMH source is here: https://github.com/snazy/cassandra/blob/8714-row-cache-jemalloc/test/microbench/org/apache/cassandra/test/microbench/OffHeapAllocationBench.java

Robert Stupp

More information about the hotspot-dev mailing list