[gclist] GC and JNI

Chris Dodd chrisd@reservoir.com
Thu, 29 May 2003 13:21:55 -0700 (Pacific Daylight Time)

On Fri, 30 May 2003, Zoe C. H. Yu wrote:
> Java supports JNI.  I would like to know how does Java's GC handle those
> objects created in the Java Native codes?  Do those objects can only be
> reclaimed conservatively?  Or they can be reclaimed precisely with
> compiler support?  Thanks very much!

The JNI interface insulates the GC of the JVM from the native code and
vice versa.  The native code can never see an actual pointer into the
GC heap -- instead, the JVM passes opaque handles to the native code that
cannot be directly dereferenced.  To do anything with the handle, the native
code needs to call a JVM function (through the JNI interface) to actually
do the job.

The JVM keeps track of the handles it has passed to a native method and
cleans them up when the method finishes.  If a native method wants to
keep a handle alive after it returns (to store in a global non-java
data structure for reference by later native method calls), it needs
to explicitly create a global handle (via JNI callback) and explicitly
destroy that global handle when its done.

Chris Dodd