Java 什么是“JNI 全局引用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/112603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:24:35  来源:igfitidea点击:

What is 'JNI Global reference'

javaswingjprofiler

提问by j.davies

I am using jProfiler to find memory leaks in a Java swing application. I have identified instances of a JFrame which keeps growing in count.

我正在使用 jProfiler 来查找 Java Swing 应用程序中的内存泄漏。我已经确定了数量不断增长的 JFrame 实例。

This frame is opened, and then closed.

这个框架被打开,然后关闭。

Using jProfiler, and viewing the Paths to GC Root there is only one reference, 'JNI Global reference'.

使用 jProfiler,并查看 GC Root 的路径,只有一个参考,“JNI 全局参考”。

What does this mean? Why is it hanging on to each instance of the frame?

这是什么意思?为什么它挂在框架的每个实例上?

采纳答案by Peter

Wikipedia has a good overview of Java Native Interface, essentially it allows communication between Java and native operating system libraries writen in other languages.

维基百科对Java Native Interface有一个很好的概述,本质上它允许 Java 和用其他语言编写的本机操作系统库之间的通信。

JNI global references are prone to memory leaks, as they are not automatically garbage collected, and the programmer must explicitly free them. If you are not writing any JNI code yourself, it is possible that the library you are using has a memory leak.

JNI 全局引用容易发生内存泄漏,因为它们不会自动进行垃圾收集,并且程序员必须显式释放它们。如果您自己没有编写任何 JNI 代码,则您使用的库可能存在内存泄漏。

edithereis a bit more info on local vs. global references, and why global references are used (and how they should be freed)

在这里编辑更多关于本地与全局引用的信息,以及为什么使用全局引用(以及它们应该如何被释放)

回答by erickson

A JNI global reference is a reference from "native" code to a Java object managed by the Java garbage collector. Its purpose is to prevent collection of an object that is still in use by native code, but doesn't appear to have any live references in the Java code.

JNI 全局引用是从“本机”代码到由 Java 垃圾收集器管理的 Java 对象的引用。它的目的是防止收集本机代码仍在使用的对象,但在 Java 代码中似乎没有任何活动引用。

A JFrame is a java.awt.Window, and is associated with a "native" Window object. When you are completely finished with a particular JFrame instance, you should invoke its dispose()method to clean up.

JFrame 是java.awt.Window, 并且与“本机” Window 对象相关联。当您完全处理完一个特定的 JFrame 实例时,您应该调用它的dispose()方法进行清理。

I am not sure if any native code is creating a global reference to the JFrame, but it seems likely. If it does, this will prevent the JFrame from being garbage collected. If you are creating many Windows (or subclasses) and seeing that they are never collected, make sure that they are disposed.

我不确定是否有任何本机代码正在创建对 JFrame 的全局引用,但似乎很有可能。如果是,这将阻止 JFrame 被垃圾收集。如果您正在创建许多 Windows(或子类)并发现它们从未被收集,请确保它们已被释放。

回答by Maximilian Skog

I had this exact problem when fixing memory leaks in a JavaFX application. In the end the problem turned out to be that i was running the application in debug modeand had several breakpoints in the code. This seems to have caused objects to be 'JNI Global reference' and being kept in memory without apparent reason. When i turned off the debug mode everything worked as it should!

在修复 JavaFX 应用程序中的内存泄漏时,我遇到了这个确切的问题。最后,问题是我在调试模式下运行应用程序并且代码中有几个断点。这似乎导致对象成为“JNI 全局引用”并在没有明显原因的情况下保存在内存中。当我关闭调试模式时,一切正常!