You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@harmony.apache.org by "bootjvm@earthlink.net" <bo...@earthlink.net> on 2005/10/28 23:23:40 UTC

Example GC algorithm

All,

Sometimes the best explanation is a good example.
I have written a simple GC algorithm that counts
references to objects as an example of how the
GC interface might be implemented.  Some algorithms
may not use all the hooks, perhaps some might need
an additional hook or two beyond what is currently
defined, but this example should give the idea of the
rationale behind the GC methodology.

Look into 'jvm/src/object.c' for object_instance_new()
and object_instance_delete() for the GC hooks that
will be needed for most Java bytecode 'new' events
and for collection.

The source file 'jvm/src/gc_refcount.c' implements
the GC API of 'jvm/src/gc.h' and is configured from
'config.sh' with a new GC option 'refcount.  When any
GC_xxx() macro is called, then this code will be
called when so configured.

When a 'NEW' bytecode (still under construction)
or related opcodes requests a new object to be
created, a reference to it will also soon be
created.  When this happens, the GC counts it (+1).
As many references may be created as needed by
the Java program.  When a reference variable is
not needed or is modified with another object's
reference, the reference count goes down (-1).
When the count reaches zero, the object status
bit OBJECT_STATUS_GCREQ is set and the
GC_RUN() macro will collect it when invoked
at some point in the future.

Of course, there are better GC algorithms than
simple reference counts, but this should give the
idea of the rationale behind the GC interface.
As usual, comments and questions are welcome.


Dan Lydick



Dan Lydick