Download presentation
Presentation is loading. Please wait.
Published byCharlene Stevens Modified over 9 years ago
1
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques
2
2/4/20162 Contents Motivation Mark-scan Two-space copying Reference counting Comparison
3
2/4/20163 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
2/4/20164 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
2/4/20165 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
2/4/20166 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/4/20167 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
2/4/20168 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
2/4/20169 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
2/4/201610 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
2/4/201611 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
2/4/201612 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
2/4/201613 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
2/4/201614 Cyclic structures in heap: Mark-scan: unaffected Copying: unaffected Reference count: needs special attention!
15
2/4/201615 Summary Motivation Mark-scan Two-space copying Reference counting Comparison
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.