How can I pin a byte array in Java?

arrays, garbage-collection, java, memory-management

Solution

You can use a ByteBuffer/allocateDirect() This creates a byte buffer which is in the "c" space and doesn't use heap so it won't get moved and can be use efficiently with JNI calls.

Problem

Is there a way to pin a byte array in java, so it never gets moved/compacted? I am working on an application which is intended to have zero GCs during runtime, and I want to use primitive byte arrays that are pinned to a memory mapped area. Is there any way to do this or hack my way to it?

Original source