Synchronization between JNI & GC

Krystal Mok rednaxelafx at gmail.com
Wed Apr 2 21:40:09 UTC 2014


Hi Jungwoo,

To my knowledge, the only way JNI synchronizes with GC in HotSpot is by the
means of the GC locker and safepoint checks upon entry to VM.

When JNI code enters a critical section (e.g. after
calling GetPrimitiveArrayCritical() but before calling
ReleasePrimitiveArrayCritical()), HotSpot will activate the GC locker,
meaning it does not allow a GC at the moment. If a GC is requested when the
GC locker is active, HotSpot will try to expand the heap to fulfill the
allocation request instead of doing a GC.
When JNI exits all critical sections, the GC locker is inactivated. If
there had been a GC request when the GC locker was active, then a major GC
will be started at GC locker inactivation time.
-XX:+GCLockerInvokesConcurrent overrides that behavior to start a
concurrent GC cycle instead of a full GC.

In the general case, JNI synchronizes with the GC by safepoints. When JNI
calls into the VM (via JNI Invocation API) or returns to the VM, there are
safepoint checks at the entry point, so that if a GC safepoint is active,
it shouldn't run past the safepoint until GC completes. That's how the
assert you mentioned could work.

BTW, Universe::heap()->is_gc_active() only checks if there's a
stop-the-world GC activity happening right now. It doesn't care about the
concurrent phases.

Hope it helps,
Kris


On Wed, Apr 2, 2014 at 2:00 PM, Jungwoo Ha <jwha at google.com> wrote:

> Is there a good write up on how the synchronization happen between JNI and
> GC?
> I am looking at the NewGlobalRef code but it has assert statement like
> this:
>
> assert(!Universe::heap()->is_gc_active(), "can't extend the root set
> during GC");
>
> But how is this enforced by the JNI code calling NewGlobalRef?
>
> Thanks,
> Jungwoo
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/hotspot-gc-dev/attachments/20140402/79209533/attachment.htm>


More information about the hotspot-gc-dev mailing list