pub struct Global<T>where
T: Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync + 'static,{ /* private fields */ }Expand description
A global reference to a Java object.
Global references are slower to create and delete than ordinary local references, but have several properties that distinguish them:
-
Global references are not bound to the lifetime of a
Env. -
Global references are not bound to any particular thread; they have the
SendandSynctraits. -
Until a global reference is dropped, it will prevent the referenced Java object from being garbage collected.
-
It takes more time to create or delete a global reference than to create or delete a local reference.
These properties make global references useful in a few specific situations:
-
When you need to keep a reference to the same Java object across multiple invocations of a native method, especially if you need a guarantee that it’s the exact same object every time, one way to do it is by storing a global reference to it in a Rust
staticvariable. -
When you need to send a Java object reference to a different thread, or use a Java object reference from several different threads at the same time, a global reference can be used to do so.
-
When you need a Java object to not be garbage collected too soon, because some side effect will happen (via
java.lang.Object::finalize,java.lang.ref.Cleaner, or the like) when it is garbage collected, a global reference can be used to prevent it from being garbage collected. (This hold is released when the global reference is dropped.)
See also Weak, a global reference that does not prevent the underlying
Java object from being garbage collected.
§Creating and Deleting
To create a global reference, use the Env::new_global_ref method or wrap
an existing global reference (that you own) with Env::global_from_raw.
To delete it, simply drop the Global.
If you have a raw jobject global reference that you do not own, you can
use Env::as_cast_raw to create a temporary wrapper that will not delete
the reference when dropped.
Note that, because global references take more time to create or delete than local references do, they should only be used when their benefits outweigh this drawback. Also note that this performance penalty does not apply to using a global reference (such as calling methods on the underlying Java object), only to creation and deletion of the reference.
§Warning: Drop On an Attached Thread If Possible
When a Global is dropped, a JNI call is made to delete the global
reference. If this frequently happens on a thread that is not already
attached to the JVM, the thread will be temporarily attached using
JavaVM::attach_current_thread_for_scope, causing a severe performance
penalty.
To avoid this performance penalty, ensure that Globals are only dropped on
a thread that is already attached (or never dropped at all).
In the event that a global reference is dropped on an unattached thread, a
message is logged at log::Level::Warn.
Implementations§
Source§impl<T> Global<T>
impl<T> Global<T>
Sourcepub fn null() -> Self
pub fn null() -> Self
Creates a Global wrapper for a null reference
This is equivalent Global::default()
Sourcepub fn into_raw(self) -> jobject
pub fn into_raw(self) -> jobject
Unwrap to the auto-delete global reference into a raw global jobject.
This prevents the global reference from being automatically deleted when this guard is dropped.
§Leaking References
When unwrapping a Global you should consider how else you will
ensure that the reference will get deleted.
The global reference may end up leaking unless a new Global wrapper
is create later, or you find some way to call the JNI DeleteGlobalRef
API on the raw reference.
Note: You should not find yourself unwrapping a raw global jobject
that you do not logically own. If you have a raw global jobject that
you do not own, you should instead use Env::as_cast_raw to create a
temporary wrapper.
Trait Implementations§
Source§impl<T> Reference for Global<T>
impl<T> Reference for Global<T>
Source§type Kind<'env> = <T as Reference>::Kind<'env>
type Kind<'env> = <T as Reference>::Kind<'env>
Self::Kind type corresponds to the underlying
class type (such as JObject or JString), parameterized by the
lifetime that indicates whether the type holds a global reference
('static) or a local reference that’s tied to a JNI stack frame. Read moreSource§type GlobalKind = <T as Reference>::GlobalKind
type GlobalKind = <T as Reference>::GlobalKind
GlobalKind type should be equivalent to
Kind<'static>, with the additional bound that ensures the type is
Send + Sync Read moreSource§fn as_raw(&self) -> jobject
fn as_raw(&self) -> jobject
crate::sys::jobject reference.Source§fn class_name() -> Cow<'static, JNIStr>
fn class_name() -> Cow<'static, JNIStr>
Source§fn lookup_class<'caller>(
env: &Env<'_>,
loader_context: &LoaderContext<'_, '_>,
) -> Result<impl Deref<Target = Global<JClass<'static>>> + 'caller>
fn lookup_class<'caller>( env: &Env<'_>, loader_context: &LoaderContext<'_, '_>, ) -> Result<impl Deref<Target = Global<JClass<'static>>> + 'caller>
Source§unsafe fn kind_from_raw<'env>(local_ref: jobject) -> Self::Kind<'env>
unsafe fn kind_from_raw<'env>(local_ref: jobject) -> Self::Kind<'env>
Self::Kind for the given reference that is tied
to the specified lifetime. Read moreSource§unsafe fn global_kind_from_raw(global_ref: jobject) -> Self::GlobalKind
unsafe fn global_kind_from_raw(global_ref: jobject) -> Self::GlobalKind
Source§fn null<'any>() -> Self::Kind<'any>
fn null<'any>() -> Self::Kind<'any>
null reference based on Self::Kind