A method and system for performing generational garbage collection on a
virtual heap in a virtual machine is provided. The garbage collection
method is suited for use with small consumer and appliance devices that
have a small amount of memory and may be using flash devices as persistent
storage. The garbage collection method may provide good performance where
only a portion of the virtual heap may be cached in the physical heap. The
virtual heap may use a single address space for both objects in the store
and the in-memory heap. In one embodiment, a single garbage collector is
run on the virtual heap address space. The garbage collection method may
remove non-referenced objects from the virtual heap. The garbage
collection method may also include a compaction phase to reduce or
eliminate fragmentation, and to improve locality of objects within the
virtual heap. In one embodiment, the garbage collector for the virtual
heap may be implemented as a generational garbage collector using working
sets in the virtual heap, where each generation is confined to a working
set of the heap. The generational garbage collector may allow the flushing
of changes after each garbage collection cycle for each working set
region. Heap regions with different flushing policies may be used. An
object nursery region without flushing where objects are initially created
may be used. When a garbage collection cycle is run, objects truly
referenced in the object nursery may be copied back into heap regions to
be flushed, while short-lived objects no longer referenced may be deleted
without flushing.