如果调用JNI DeleteGlobalRef(),相应的java对象是否会被垃圾回收? [英] If JNI DeleteGlobalRef() is called, does the corresponding java object get garbage collected?

查看:1094
本文介绍了如果调用JNI DeleteGlobalRef(),相应的java对象是否会被垃圾回收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,如果我在C ++中创建一个全局引用jobject,然后将其传递给某些Java代码,并删除调用DeleteGlobalRef(),那么底层的java对象是否可能立即被垃圾收集,以便任何未来已经引用该对象的Java代码可能会返回NullPointerException?具体来说,如果我有一些C ++代码执行类似这样的简化示例:

What I mean is, if I create a global reference jobject in C++, and then pass that off to some Java code, and delete call DeleteGlobalRef(), is that underlying java object potentially garbage collected right away, so that any future Java code that is already refering to that object might come back with a NullPointerException? Specifically, if I have some C++ code that does something like this simplified example:

static jobject myObjGlobalRef;
static JNIEnv* env = /* call to create a JVM and get the JNI env */;
jobject ReturnMyObj()
{
    /* <<Code that defines class, constructorId, and param1 goes here>> */

    jobject localObj = env->NewObject(class, constructorId, param1);
    myObjGlobalRef = env->NewGlobalRef(localObj);
}

void DeleteMyObj()
{
    env->DeleteGlobalRef(myObjGlobalRef);
}

myObjGlobalRef = ReturnMyObj();

jobject otherExistingGlobalRef = /* Assume we get another global ref to another parent obj somewhere else */
/*  ... other code here ... */
// Invoke some method on some other pre-existing global reference that uses
// myObjGlobalRef,  lets assume this method stores the object referenced by
// myObjGlobalRef into a parent object referenced by otherExistingGlobalRef:
env->CallVoidMethod(env, otherExistingGlobalRef, addASubObjectMethodId, myObjGlobalRef);

DeleteMyObj();

// Does the pointed to by myObjGlobalRef still exist here, assuming that
// otherExistingGlobalRef now references it?

这在JNI中如何运作?对象上的GlobalReference只是对象的引用计数,因此如果我释放GlobalReference jobject,它不一定是垃圾收集底层java对象,直到所有引用它(例如父对象otherExistingGlobalRef引用它已经消失了?

How does this work in JNI? Is a "GlobalReference" on an object just a reference count to the object, so that if I free the GlobalReference jobject, it does not necessarily garbage collect the underlying java object until all references to it (such as the "parent" object otherExistingGlobalRef referencing it) are gone?

如果你能回答这个并提供一些官方Java / Sun / Oracle文档的链接来备份你的答案,你将获得奖励:-) 。

If you can answer this and provide a link to some official Java/Sun/Oracle documentation backing up your answer, you earn bonus kudos :-).

推荐答案

DeleteGlobalRef()释放引用,而不是对象。

DeleteGlobalRef() frees the reference, not the object.

如果这是最后一次可到达的引用,那么引用的对象可用于垃圾回收。

If that was the last reachable reference, then the referenced object is available for garbage collection.


对象上的GlobalReference只是对象的引用计数

Is a "GlobalReference" on an object just a reference count to the object

否。它只是一个有效的参考,直到您明确释放它为止。 Java的垃圾收集根本不依赖于引用计数。

No. It's just a reference that remains valid until you free it explicitly. Java's garbage collection does not rely on reference counts at all.

另见 Oracle关于全球参考文献的文档

这篇关于如果调用JNI DeleteGlobalRef(),相应的java对象是否会被垃圾回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆