[英]NewGlobalRef of a weak reference still prevent a object from garbage collected
To implement a callback function from the native code to Java code, I have to create a global reference using NewGloabRef
.为了实现从本机代码到 Java 代码的回调 function,我必须使用
NewGloabRef
创建一个全局引用。 From the memory profile, I found that,once I called env->NewGlobalRef(weak_this)
, even it was a weak reference of the player object, the Player object will be available as Root Objects, which I think will prevent it from being garbage collected.从 memory 配置文件中,我发现,一旦我调用
env->NewGlobalRef(weak_this)
,即使它是播放器 object 的弱引用,播放器 ZA8CFDE6331BD59EB2AC96F8911C4B6 将阻止它作为垃圾可用集。
But my understanding is the weak reference won't prevent the object from garbage collected.但我的理解是弱引用不会阻止 object 被垃圾收集。
//java code
Player{
native_init(new WeakReference(this)),
}
//JNi code
//listener
Listener::Listener(jobject weak_this)
{
//will use mObject for callback
mObject = env->NewGlobalRef(weak_this);
}
xxxx_Player_native_init(xxxx. Object weak_this)
{
Listener l = new Listener(weak_this);
}
EDIT:编辑:
memory profile: memory简介:
Root Object 0x2C820E10 <com/trident/tv/media/player/JniTPlayer>
com/trident/tv/media/player/JniTPlayer.trace : 0x2C83CC54 <java/lang/String>
com/trident/tv/media/player/JniTPlayer.listenerList : 0x2C820E64 <java/util/Vector>
log of JNI JNI的日志
[JNI] NewGlobalRef(0x2C820E10 [com/trident/tv/media/player/JniTPlayer]) : 0x2C820E10
A WeakReference
is a Java object with an ordinary reference to it. WeakReference
是 Java object 对它的普通引用。 It contains a reference to another object.它包含对另一个 object 的引用。 It is the contained reference that is "weak", not the reference to the
WeakReference
itself. “弱”的是包含的引用,而不是对
WeakReference
本身的引用。
So when you call env->NewGlobalRef(weak_this)
(assuming weak_this
is a WeakReference
), the effect is the same as assigning weak_this
to a static.所以当你调用
env->NewGlobalRef(weak_this)
(假设weak_this
是一个WeakReference
)时,效果与将weak_this
分配给一个 static 是一样的。 It doesn't cause the object reference contained by the WeakReference
to be strongly reachable.它不会导致
WeakReference
包含的 object 引用可强访问。
I think you may be misinterpreting what the memory profiler is telling you.我认为您可能误解了 memory 分析器告诉您的内容。 In particular, I would expect it to show the contained reference of a
WeakReference
to be reachable... up until the GC decided to break the link.特别是,我希望它显示
WeakReference
的包含引用是可访问的......直到 GC 决定断开链接。 Try an experiment with an WeakReference
in an ordinary static
variable.尝试在普通
WeakReference
变量中使用static
进行实验。
UPDATE更新
I'm starting to think that this is normal behaviour for JNI NewGlobalRef
.我开始认为这是 JNI
NewGlobalRef
的正常行为。 The JNI documentation is (as always) very vague about the method's behaviour. JNI 文档(一如既往)对该方法的行为非常模糊。
Note that there is also a JNI method called NewGlobalWeakRef
;请注意,还有一个名为
NewGlobalWeakRef
的 JNI 方法; see http://java.sun.com/docs/books/jni/html/refs.html#27531 .请参阅http://java.sun.com/docs/books/jni/html/refs.html#27531 。 If nothing else,
NewGlobalWeakRef
provides an alternative way of doing what you are trying to do.如果没有别的,
NewGlobalWeakRef
提供了另一种方法来做你想做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.