简体   繁体   English

JNI中的GlobalReferences是否可以防止GC清理对象?

[英]Do GlobalReferences in JNI prevent GC from cleaning up object?

According to this and this , GlobalReferences to objects in JNI, keep a reference of the object and all subfields accessible. 根据这个这个 ,GlobalReferences在JNI对象,保留对象的引用和所有子域访问。 And it is a memory leak not to manually free it promptly. 而且这是内存泄漏,不及时手动释放它。

What is not clearly stated is if a global reference prevents the object from being GC'd, or the leak is in the reference itself. 没有明确说明的是,如果全局引用阻止对象被GC填充,或者泄漏是在引用本身中。 In other words I am trying to understand if a GlobalReference will stop the object to be GC'ed ever, like if I had an extra reference for it in the Java space, or if the obejct could be GC'd but the leak is in the reference object itself. 换句话说,我试图了解GlobalReference是否会停止将对象进行GC处理,例如,如果我在Java空间中对此对象有了额外的引用,或者对象是否可以被GC处理但泄漏仍在参考对象本身。

I tend to believe is the former, since the weak global reference exists, which according to the docs will let the GC original object die - but I decided to ask to be on the safe side. 我倾向于认为是前者,因为存在弱的全局引用,根据文档,这将使GC原始对象死亡-但我决定要求保持安全。

if a GlobalReference will stop the object to be GC'ed 如果GlobalReference将停止要进行GC处理的对象

Yes, that is the case. 是的,就是这种情况。 The GC is aware of JNI global references and considers them as GC roots. GC知道JNI全局引用,并将其视为GC的根。

JNI global references are also exposed in the JVMTI and you can see them in tools like JProfiler. JNI全局引用也在JVMTI中公开,您可以在JProfiler之类的工具中看到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM