JNI Object | Use across JNI calls? | Use on different thread? | Notes |
---|---|---|---|
JavaVM |
Yes | Yes | To obtain an instance of JavaVM , either:
JavaVM .
|
JNIEnv |
Yes | No |
A JNIEnv is valid across JNI calls, but only on its original thread. If you are going to cache a JNIEnv , consider using a thread_local or similar.
If a
Otherwise, you don't need to clean up a |
Primitive value types
|
Yes | Yes | These are just aliases for normal native value types, so there are no special lifetime or cleanup considerations. |
Reference types
|
No (local refs) | No (local refs) |
If one of these is passed in as an argument to a JNI call, or returned from another JNI API, it is typically a local reference
and is only valid for the duration of that JNI call, on that same thread. These do not require any cleanup.
A durable global reference can be created from any local reference by calling |
Yes (global refs) | Yes (global refs) | ||
ID types
|
Yes | Yes | These are durable and can be used without restriction. No cleanup is required. |
Last active
January 21, 2023 21:29
-
-
Save latkin/4a11ac2edb502c4a9141 to your computer and use it in GitHub Desktop.
JNI Object Lifetimes Quick Reference
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment