Native Code Interface Specification
 In this topic

*Goals

*Garbage Collection Mechanism

 

Java & Native Code    PreviousRNI
Native Code Interface Specification     Previous RNI

 


Overview of the Garbage Collection Architecture

These sections describe the goals and implementation details of garbage collection.

Goals

  1. Garbage collection should be transparent for replacing malloc-based C programs.
  2. Garbage collection overhead should be minimal (in terms of time and percentage execution) for most Java applets and applications.

Garbage Collection Mechanism

The key to countering the overhead that occurs while determining the difference between live and dead pointers is to make memory allocation very efficient. Most modern, high performance garbage collectors start with a simple way of allocating memory. To allocate, a current block of memory exists with a part that's used and a part that's free. The new object is added on the end of the used block, and the pointer in the used block is moved. Another block is dynamically allocated when the current block becomes full. The blocks are a fixed size. If you need a larger block ("large" is dependent on tuning; this will typically be between 4K and 8K), a custom block is allocated that has only that object. All the blocks are linked together. The decision about the size of the block is the same trade-off as page size within the operating system.

To distinguish live pointers from dead pointers, we start from a root set where we know there are live pointers. These pointers are followed by copying them into new blocks. Each object that these pointers point to in these roots is worked through recursively. When the end of scanning the new objects in the new copy is reached, every live object has been copied. This scheme can be fairly expensive if you always copy many objects that are very stable. This is improved by taking a snapshot of the last point that was garbage collected and the new point that is garbage collected and copy only the objects that are in between, leaving old objects alone. This is called generational GC, using two generations, in this case.

The large blocks are not copied, they're just logically moved from the old set to the new. Further improvements may be made by considering only roots between the phases the part of memory that has been modified.

Garbage collection is called on allocation of new objects. When a new object is allocated, the garbage collector detects memory threshold (a heuristic is used to decide if it needs to garbage collect and, if so, sets a flag). The VM looks at the flag and initiates the process of checking the block count.

Mark and compact are not used as a garbage collection method, because they are inherently less efficient in situations where you have lots of garbage, which will be typical in Java programs.

Top © 1998 Microsoft Corporation. All rights reserved. Terms of use.