CS453 Colorado State University ========================================== Garbage Collection project ========================================== 9/3/09 ------------------------ Issues for a Mark and Sweep collector 1) how do we do memory allocation? 2) when does garbage collection occur? 3) how do we compute the root set for the current run-time stack? 4) how does mark pass know what fields in objects are pointers? 5) how does the sweep pass know how big each chunk of memory is? ------------------------ Possible Solutions for Issues in Context of an Example -Look at GCtest3.java --> When do Foo objects and Node objects become garbage? --> Draw detailed stack and heap for current implementation when i==8 -In the end, there can be only MIPS. The code that does garbage collection must end up being MIPS code that is somehow incorporated into the .s file the MiniJava compiler is generating. -We can write functions in MIPS and put them in the run-time library, mips.rtl.s -The MiniJava compiler can generate MIPS code. -We can write functions in C-- and generate MIPS. ------------------------ (1) Memory Allocation --> how is allocation currently done? --> in the generated MIPS code? --> how does the MiniJava compiler generate that code? -to do GC we have to take control of the memory allocation and not depend on the system calls currently doing it --> how can we take control? --> if we are in control, how do we allocate memory? -bump pointer -free lists -free list next pointer concept -initializing the memory allocation data structures data structures global pointer to first byte of heap global integer with heap size global bump pointer global pointer to first node in free list --> how could we initialize those variables? ------------------------ (2) Garbage Collection Trigger -should happen when there is no more memory left to allocate --> given bump ptr and free list schemes when is that the case -MiniJava compiler already generating calls to halloc, let's just write a halloc_gc ------------------------ (3) Root Set --> what are the root pointers when we are executing testing method? -requirements -when call halloc_gc need to pass in info to indicate which procedure is calling halloc_gc -halloc_gc needs to pass that info to garbage collector -garbage collector needs to figure out root set based on what function activation records are on the stack -procedure pointer map concept -pass current frame pointer into halloc_gc -some set distance from where frame pointer is pointing put id that indicates what procedure the activation record is for -use that id to search a global list of procedure pointer maps -the MiniJava compiler has to generate the pointer map list and then pass in a pointer to the head of the list to the function that initialized the gc ------------------------ (4) Mark Pass --> what more do we have to add to objects to enable the mark pass? -class descriptor concept -> show them an example descriptor that the MiniJava compiler could generate --> how could we change fields prepended for each object? ------------------------ (5) Sweep Pass --> how does the sweep pass know how big each chunk of memory is? -------------------------- Step through full example in GC-example.pdf -------------------------- GC-start.c -void pointer weirdness -ptrmap and descriptor helper routines -traversing the run-time stack ------------------------------ Outline of a possible approach init_gc allocate heap initialize various global variables like GC_freelist halloc_gc if still in bump ptr phase if space use bump ptr to allocate requested space PLUS extra fields else collect garbage else if can find allocation on free list return pointer to data part of that else collect garbage if can find allocation on free list return pointer to data part of that else fail collect garbage find the root set mark all reachable allocations sweep unreachable allocations onto freelist find root set loop: find the ptrmap for gc_key determine addresses for all parameters and locals that are ptrs find fp for next frame in stack goto loop ------------------------ mstrout@cs.colostate.edu, 9/3/09, 9/7/09