public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
* CNI and garbage collection
@ 2008-12-13 22:56 Daniel Walter
  2008-12-13 23:22 ` Glenn Chambers
  0 siblings, 1 reply; 7+ messages in thread
From: Daniel Walter @ 2008-12-13 22:56 UTC (permalink / raw)
  To: java

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?

Questions on GC.
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?


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.

Daniel


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: CNI and garbage collection
  2008-12-13 22:56 CNI and garbage collection Daniel Walter
@ 2008-12-13 23:22 ` Glenn Chambers
  2008-12-14 10:55   ` Andrew Haley
  0 siblings, 1 reply; 7+ messages in thread
From: Glenn Chambers @ 2008-12-13 23:22 UTC (permalink / raw)
  To: Daniel Walter; +Cc: java

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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: CNI and garbage collection
  2008-12-13 23:22 ` Glenn Chambers
@ 2008-12-14 10:55   ` Andrew Haley
  0 siblings, 0 replies; 7+ messages in thread
From: Andrew Haley @ 2008-12-14 10:55 UTC (permalink / raw)
  To: Glenn Chambers; +Cc: Daniel Walter, java

Glenn Chambers wrote:
> 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.

I think a bit more clarification is required.

If you get a reference to a Java object as a result of a function call, you
must store that reference somewhere that is reachable by the collector or
that object will be collected.  The collector scans all fields of all objects,
so something like

    MyClass::foo = objectRef;

is sufficient.  Storing the reference in an object in the C++ heap will *not*
do.

Andrew.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: CNI and garbage collection
  2001-07-19 19:30 ` Bryce McKinlay
@ 2001-07-19 19:49   ` Marcus G. Daniels
  0 siblings, 0 replies; 7+ messages in thread
From: Marcus G. Daniels @ 2001-07-19 19:49 UTC (permalink / raw)
  To: Bryce McKinlay, Jeff Sturm; +Cc: java

>>>>> "BM" == Bryce McKinlay <bryce@waitaki.otago.ac.nz> writes:

BM> So how to keep a pointer, associated with a given class or object,
BM> to your native object? One solution is to declare a field (in
BM> Java) of the type gnu.gcj.RawData. This class is treated specially
BM> by the garbage collector, and can be used for storing a pointer to
BM> some native, non-Java object. RawData fields will not be marked by
BM> the GC, so you need to dealocate them explicitly (perhaps from a
BM> finalizer).

Works like a charm.  Thanks very much. 

MD> But what about the C++ objects inside of the Java object?  Is
MD> there a special way to allocate them?

>>>>> "JS" == Jeff Sturm <jsturm@one-point.com> writes:

JS> You could try to override the "new" operator to place C++
JS> objects where they will be traced.  But that exposes details of the
JS> collector implementation, and could have other pitfalls when done
JS> together with libgcj.  See boehm-gc/include/gc_cpp.h for more info.

Cool, thanks for the pointer.  (And to both of you for pointing out that
Boehm GC doesn't move things!)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: CNI and garbage collection
  2001-07-19 18:54 Marcus G. Daniels
  2001-07-19 19:30 ` Bryce McKinlay
@ 2001-07-19 19:34 ` Jeff Sturm
  1 sibling, 0 replies; 7+ messages in thread
From: Jeff Sturm @ 2001-07-19 19:34 UTC (permalink / raw)
  To: Marcus G. Daniels; +Cc: java

On 19 Jul 2001, Marcus G. Daniels wrote:
> The problem seems to be that garbage collection is moving my Java
> object, but the ivars aren't following.

Well, boehm-gc isn't a moving collector, but it is possible you have
objects that are swept prematurely.

If a C++ object contains references to Java objects, they won't be seen by
the collector.

> But what about the C++ objects inside of the Java object?  Is there a special
> way to allocate them?

I don't know.  You could try to override the "new" operator to place C++
objects where they will be traced.  But that exposes details of the
collector implementation, and could have other pitfalls when done
together with libgcj.  See boehm-gc/include/gc_cpp.h for more info.

Jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: CNI and garbage collection
  2001-07-19 18:54 Marcus G. Daniels
@ 2001-07-19 19:30 ` Bryce McKinlay
  2001-07-19 19:49   ` Marcus G. Daniels
  2001-07-19 19:34 ` Jeff Sturm
  1 sibling, 1 reply; 7+ messages in thread
From: Bryce McKinlay @ 2001-07-19 19:30 UTC (permalink / raw)
  To: Marcus G. Daniels; +Cc: java

"Marcus G. Daniels" wrote:

> I've got a Java class from which I generate headers using gcjh.  I'm
> using the -prepend and -add features to insert declarations for ivars
> types, and then the ivars themselves.  In a CNI native method I have
> some initialization code for setting up the C++ side of things.  The
> problem seems to be that garbage collection is moving my Java object,
> but the ivars aren't following.  I.e. if I disable garbage collection
> the program runs fine.

So, you're adding extra fields in the CNI header for a Java class? You can't do
that. The size of the object as seen by the java compiler vs C++ will not match.
"new" will be allocating space which is smaller than the real size of the object
and C++ will be overwriting memory which does not belong to the object. It is
probably just luck that it seems to work with the null GC.

The Boehm GC never moves objects.

> In the CNI documentation,
>
>   http://gcc.gnu.org/java/papers/cni/t1308.html
>
> it says that "The C++ compiler is smart enough to realize the class is
> a Java class, and hence it needs to allocate memory from the garbage
> collector."
>
> But what about the C++ objects inside of the Java object?  Is there a special
> way to allocate them?

Using "new" in C++ will use Java GC allocation (_Jv_AllocObject) if the class in
question is a Java type (declared within an `extern "java"' block). For all
other types, the regular C++ new implementation  is used, which means you have
to deallocate explicitly if you dont want memory leaks.

So how to keep a pointer, associated with a given class or object, to your
native object? One solution is to declare a field (in Java) of the type
gnu.gcj.RawData. This class is treated specially by the garbage collector, and
can be used for storing a pointer to some native, non-Java object. RawData
fields will not be marked by the GC, so you need to dealocate them explicitly
(perhaps from a finalizer).

Alternatively, if you dont want to have to do the explicit dealocation, you can
just create a special java.lang.Object field, and allocate your object using
_Jv_AllocBytes. This way the GC will still mark your native data but will know
not to expect a regular Java object.

Note however that the memory allocated with _Jv_AllocBytes is not itself scanned
for pointers, so you generally should not keep references to GC allocated
objects in there.

regards

  [ bryce ]


^ permalink raw reply	[flat|nested] 7+ messages in thread

* CNI and garbage collection
@ 2001-07-19 18:54 Marcus G. Daniels
  2001-07-19 19:30 ` Bryce McKinlay
  2001-07-19 19:34 ` Jeff Sturm
  0 siblings, 2 replies; 7+ messages in thread
From: Marcus G. Daniels @ 2001-07-19 18:54 UTC (permalink / raw)
  To: java

I've got a Java class from which I generate headers using gcjh.  I'm
using the -prepend and -add features to insert declarations for ivars
types, and then the ivars themselves.  In a CNI native method I have
some initialization code for setting up the C++ side of things.  The
problem seems to be that garbage collection is moving my Java object,
but the ivars aren't following.  I.e. if I disable garbage collection
the program runs fine.

In the CNI documentation,
 
  http://gcc.gnu.org/java/papers/cni/t1308.html

it says that "The C++ compiler is smart enough to realize the class is
a Java class, and hence it needs to allocate memory from the garbage
collector."  

But what about the C++ objects inside of the Java object?  Is there a special
way to allocate them?

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-12-14 10:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-13 22:56 CNI and garbage collection Daniel Walter
2008-12-13 23:22 ` Glenn Chambers
2008-12-14 10:55   ` Andrew Haley
  -- strict thread matches above, loose matches on Subject: below --
2001-07-19 18:54 Marcus G. Daniels
2001-07-19 19:30 ` Bryce McKinlay
2001-07-19 19:49   ` Marcus G. Daniels
2001-07-19 19:34 ` Jeff Sturm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).