Hacker News new | past | comments | ask | show | jobs | submit login

- You cannot get the native address of arrays in order to pass them to native code.

- Arrays are subject to being moved by the GC, so even if you could get the address they could move.

- Arrays are limited to around 16 GB, and that's if you're packing data into longs.

- Accessing an array involves a bounds check, which may float out of a loop, but may well not.

- Arrays cannot be allocated with memory that is anything except mapped private.

I use Unsafe to put memory into a location where I can get a stable native address in order to make native calls using it. Using an array would not allow that.




> Arrays are limited to around 16 GB

On HotSpot arrays are limited to 2 GB


Correct, however Hotspot isn't the only JIT compiler around in the Java world.

At least J9, Azul and now Graal, and then there is the plethora of embedded ones, but those ones wouldn't be able to use that much memory anyway.


It's not a JIT problem, it's a VM problem. Graal uses the same VM with a different (or additional JIT) JIT and inherits the same (intentional) 2 GB limitations.

While J9 can give you larger arrays AFAIK you're not guaranteed to get a single, contiguous memory region. You won't notice in Java but JNI criticals may return copies.

I haven't tested Zing, but Zulu is just HotSpot.


I believe that allocating a native byte buffer gets around all those issues?


Yes, but then you can just varhandles on the buffer.


I'm not sure what you're getting at. Why would you want to? The issue was having a big chunk of native memory that you can pack data into on the java side and read contiguously without barriers on the native side and that's what a native allocated byte buffer would get you. Or am I missing something? Not sure what varhandles adds to the situation? Sorry if I'm being dense :o)




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: