Why do we need DeleteWeakGlobalRef?

P

pookiebearbottom

What purpose does it serve? If I do not use it, my objects will still
be garbage collected, right? Is there a memory leak on the C/C++ side
if I do not call it?
 
X

xarax

What purpose does it serve? If I do not use it, my objects will still
be garbage collected, right? Is there a memory leak on the C/C++ side
if I do not call it?

Seems like a JNI question. References to objects from
JNI routines must be "locked" to prevent garbage collection.
There are a few ways to do that, and each must be undone
before returning to the JVM from the JNI function. Otherwise,
you'll have a dangling locked reference that won't get GC'd.
See the JNI documentation for more details and "got-cha's".
 
C

Chris Uppal

What purpose does it serve? If I do not use it, my objects will still
be garbage collected, right? Is there a memory leak on the C/C++ side
if I do not call it?

If you don't call it then the JNI mechanisms for tracking references from the
C-side to the Java side will leak memory.

-- chris
 
P

pookiebearbottom

But I thought the idea of the weak reference is that the java side can
clean it up if the only references to the object are weak.
 
X

xarax

But I thought the idea of the weak reference is that the java side can
clean it up if the only references to the object are weak.

You are comparing Apples to Oranges. Totally different
concepts. JNI references are entirely different from
Reference objects; they have absolutely nothing to do
with each other, other than the garbage collector cares
about them, but in entirely different ways.
 
C

Chris Uppal

But I thought the idea of the weak reference is that the java side can
clean it up if the only references to the object are weak.

The Java side can certainly GC the object itself. However, as I said before,
it's "the JNI mechanisms for tracking references from the C-side to the Java
side" that don't get cleaned up.

Forget weak refs for a second and consider a "global" JNI reference (which are
somewhat simpler than the "normal" thread-local references). The Java GC
cannot "see" references to Java objects that are held by C code -- therefore it
doesn't even try to do so. The JVM does not provide a pointer directly to the
Java object itself (a pointer to the memory it occupies), instead it provides a
"handle" (or you could even consider it to be a kind of Proxy) for the object,
and it is that that the C code sees. Somewhere inside the JNI implementation
there is a mapping from the handles to the actual Java objects, and the JVM's
GC is aware of those tables, and uses them (amongst other things) to decide
which Java objects are eligible for reaping. If the C code does not release
those handles properly, then there will be /two/ space leaks, one is that the
Java objects themselves won't be collected, the other is that the handles
themselves (and the corresponding table space) won't be released in the
internals of JNI.

(Of course, the above is only an approximation -- I don't know exactly what any
real implementation does, but the JNI bit can't be very different.)

Now consider weak references. Exactly the same issues arise, but in this case
the GC knows that the mapping tables don't constitute a "strong" reference to
the Java object itself -- so that can be reaped. But the tables themselves can
only be cleaned up with cooperation from the C code. That's why you have to
release the references yourself.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top