1 / 15

GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques

GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques. Contents. Motivation Mark-scan Two-space copying Reference counting Comparison. Motivation. Beta reduction in Miranda can cause heap cells to become detached from the program graph Similar effects in Java

jstockdale
Download Presentation

GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GC16/3011 Functional ProgrammingLecture 20Garbage Collection Techniques

  2. Contents • Motivation • Mark-scan • Two-space copying • Reference counting • Comparison

  3. Motivation • Beta reduction in Miranda can cause heap cells to become detached from the program graph • Similar effects in Java • Fixed heap size – limited resources • How do we reuse (recycle) memory?

  4. We need to: • Find which cells are no longer required by the program • Make those cells available to the memory allocator • Unreferenced cells are called GARBAGE • Key technique: “GARBAGE COLLECTION” • Three popular variants: • Mark-scan • Two-space (or “semi-space”) copying • Reference counting

  5. 1. Mark-scan • Triggered when program has used >80% of heap • Evaluation suspended until collector finished • May happen many times during program • Mark: • Follow pointers from root cell of program and mark all reachable cells (set “mark bit” to 1) • Scan: • All cells in the heap • if marked, set mark bit to 0 (ready for next mark) • else attach cell to free list

  6. Mark-scan uses a FREE LIST for allocation • A linked list of cells which are available for use by the program • Before program starts, ALL cells are on free list • Then program is loaded • Then program is run • Scanner re-creates the free list by linking all unused cells • Cells are unlinked from free list when allocated

  7. 2. Two-space copying • Memory is managed as two separate heaps called FROM-space and TO-space • At first, program is loaded and run in FROM-space • THERE IS NO FREE LIST – just a pointer to the top of allocated memory in FROM-space (“TOP”). • When TOP exceeds, say, 80% of FROM-space, the collector is called • Evaluation is suspended until collector finished • May happen many times during program

  8. The two-space collector must: • Follow pointers from the root cell of the program in FROM space, COPY each cell into TO-space and update any pointers to that cell so they now point to the copy in TO-space. • Swap the names/roles of FROM-space and TO-space • Allow evaluation to proceed • Garbage is “left behind” after copying • Allocation is by simple (fast) pointer-increment

  9. 3. Reference Counting • Each cell is extended with a reference count field • The reference count keeps track of how many pointers are pointing to this cell: • Each new pointer to this cell causes its reference count to be incremented • Each deletion of a pointer to this cell causes its reference count to be decremented • Any cell with a reference count of 0 is garbage. • All cells with reference count 0 are on FREE LIST

  10. Allocation is by unlinking cells from the free list • If a cell’s reference count drops to 0: • it must be put on the free list • If it contains any pointers, these must be deleted • This could cause an “avalanche” of garbage • Reference count field must normally be big enough to allow for ALL pointers to point to one cell

  11. Comparison • Overheads: • Mark-scan: • 1 bit per cell • + a stack for traversal (or use pointer-reversal) • Copying: • 100% space overhead • Reference count: • typically 32 bits per cell • + time overhead (when copy/delete pointer, must also read in and modify the cell it points to!)

  12. Compaction/fragmentation • Mark-scan: • Must run separate compactor (during scan phase) to reduce fragmentation • Copying: • Inherently compacting • No fragmentation immediately after copy (BUT remember 100% overhead) • Reference counting: • Must run separate compactor to reduce fragmentation

  13. Small program in big heap: • Mark-scan: visits ALL heap cells (slow) • Copying: only visits program cells (fast) • Reference count: only visits cells in use (fast) • Large program (nearly fills heap): • Mark-scan: performance degrades • Copying: performance degrades • Reference count: unaffected

  14. Cyclic structures in heap: • Mark-scan: unaffected • Copying: unaffected • Reference count: needs special attention!

  15. Summary • Motivation • Mark-scan • Two-space copying • Reference counting • Comparison

More Related