Chapter 20
Building a Hybrid Collector
Extend the Tutorial plan to create a ”copy-MS” collector, which allocates into a copying nursery and at collection time, copies nursery survivors into a mark-sweep space. This plan does not require a write barrier (it is not strictly generational, as it will collect the whole heap each time the heap is full). Later we will extended it with a write barrier, allowing the nursery to be collected in isolation. Such a collector would be a generational mark-sweep collector, similar to GenMS.
20.1 Add a Copying Nursery
This step will change your simple collector from using a bump pointer to a free list (but still without any garbage collection).
- In TutorialConstraints, make the following changes:
- Override the movesObjects() method to return true, reflecting that we are now building a copying collector:
- Remove the restriction on default alloc bytes (since default allocation will now go to a bump-pointed space). To do this, remove the override of maxNonLOSDefaultAllocBytes().
- Add a restriction on the maximum size that may be copied into the (default) non-LOS mature space:
- In Tutorial, add a nursery space:
- Create a new space, nurserySpace, of type CopySpace. The new space will initially be a from-space, so provide false as the third argument. Initialize the space with a contiguous virtual memory region consuming 0.15 of the heap by passing ”0.15” and ”true” as arguments to the constructor of VMRequest(more on this later). Create and initialize a new integer constant to hold the descriptor for this new space:
- Add the necessary import statements
- Add nurserySpace to the PREPARE and RELEASE phases of collectionPhase(), prior to the existing calls to msTrace. Pass true to nurserySpace.prepare() indicating that the nursery is a from-space during collection.
- Fix accounting so that Tutorial accounts for space consumed by
nurserySpace:
- Add nurserySpace to the equation in getPagesUsed()
- Since initial allocation will be into a copying space, we need to account for
copy reserve:
- Add a method to override getCollectionReserve() which returns nurserySpace.reservedPages() + super.getCollectionReserve()
- Add a method to override getPagesAvail(), returning getTotalPages() - getPagesReserved()) >> 1;
20.2 Add nursery allocation
In TutorialMutator, replace the free-list allocator (MarkSweepLocal) with a nursery allocator: Use an instance of CopyLocal, calling it nursery. The constructor argument should be Tutorial.nurserySpace:
- change alloc() to use nursery.alloc() rather than ms.alloc().
- remove the call to msSpace.postAlloc() from postAlloc() since there is no special post-allocation work necessary for the new copy space. The call to super.postAlloc() should remain conditional on allocator != Tutorial.ALLOC_DEFAULT.
- change the check within getAllocatorFromSpace() to check against Tutorial.nurserySpace and to return nursery.
- adjust collectionPhase
- replace call to ms.prepare() with nursery.reset()
- remove call to ms.release() since there are no actions necessary for the nursery allocator upon release.
20.3 Add copying to the collector
In TutorialCollector add the capacity for the collector to allocate (copy), since our new hybrid collector will perform copying.
- Add local allocators for both large object space and the mature space:
- Add an allocCopy() method that conditionally allocates to the LOS or mature space:
- Add a postCopy() method that conditionally calls LOS or mature space post-copy actions:
20.4 Make necessary changes to TutorialTraceLocal
- Add nurserySpace clauses to isLive() and traceObject():
- Add the following to isLive():
- Add the following to traceObject():
- Add a new willNotMoveInCurrentCollection() method, which identifies those objects which do not move (necessary for copying collectors):
- Modify PlanSpecificConfig to add the new nursery space:
With these changes, Tutorial should now work. You should be able to again build a BaseBaseTutorial image and test it against any benchmark. Again, if you use -X:gc:verbose=3 you can see the movement of data among the spaces at each garbage collection.