Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 'JNI Global reference'

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.

This frame is opened, and then closed.

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

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

like image 897
j.davies Avatar asked Sep 22 '08 00:09

j.davies


People also ask

What is JNI reference?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

What are local and global references in JNI?

The JNI divides object references used by the native code into two categories: local and global references. Local references are valid for the duration of a native method call, and are automatically freed after the native method returns. Global references remain valid until they are explicitly freed.

What is JNIEnv * env?

jobject NewGlobalRef(JNIEnv *env, jobject obj); Creates a new global reference to the object referred to by the obj argument. The obj argument may be a global or local reference.


3 Answers

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.

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.

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.

like image 58
erickson Avatar answered Oct 10 '22 00:10

erickson


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

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.

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

like image 36
Peter Avatar answered Oct 09 '22 22:10

Peter


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 mode and 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!

like image 15
Maximilian Skog Avatar answered Oct 09 '22 23:10

Maximilian Skog