CNI and garbage collection

Glenn Chambers gchamber@bright.net
Sat Dec 13 23:22:00 GMT 2008


On Sat, 2008年12月13日 at 17:55 -0500, Daniel Walter wrote:
> As I look through the documentation of CNI, I have not found anything about 
> how Garbage Collection is done. Is there a section on this?

It's been a few years since I actively tracked the project, so I can't
help with the documentation. I do, however remember the answers to your
questions.
> 1. In section 11.8 of the manual, a hashtable is created with new 
> java::util::Hashtable(120). How is this deallocated? Based on what I have 
> seen, it would need to be deleted in C++ which would release it to the GC to 
> decide when Java is done with it for actual destruction. Is this correct?

Java class allocation is automatically done via the Boehm garbage
collector. Invoking the 'delete' operator is not required, and will
fail. (I don't remember if it is a compile error or not.)
> 2. If I get a reference to an object as a result of a function call, how 
> does this effect Garbage Collection of this object. Do I need to delete 
> this object for Garabage Collection.

If the object is a Java object, then there will be a new reference to it
for the lifetime of the function. If the original reference is erased
in the course of program execution, the reference in the function will
prevent the object from being collected until the function exits.
If the object is a C++ object, then the normal (non-GC) memory
management model applies, and whether or not you call 'delete' depends
on the 'contract' defined by the calling method, the called method, and
the object itself. The complexity of this contractual obligation is the
basic reason that Garbage Collection was invented.
Hope that helped.


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /