Search code examples
javajava-native-interface

Which VMs or GCs support JNI pinning?


Get<PrimitiveType>ArrayElements family of functions are documented to either copy arrays, or pin them in place (and, in so doing, prevent a compacting garbage collector from moving them). It is documented as a safer, less-restrictive alternative to GetPrimitiveArrayCritical. However, I'd like to know which VMs and/or garbage collectors (if any) actually pin arrays instead of copying them.


Solution

  • G1 supports pinning as of Java 22: https://openjdk.org/jeps/423

    Shenandoah supports pinning as of Java 15: https://shipilev.net/jvm-anatomy-park/9-jni-critical-gclocker/

    In both cases it's not clear if pinning happens when using Get*ArrayElements or only when Get*Critical.

    Both GCs pin memory regions and not individual objects, so if very many regions are pinned the effect may turn out to be the same as if no pinning is done.