Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Garbage Collection

Garbage Collection
• Copying (2‐heap) GC:
– Separate heap into two large blocks
• Initially all data allocated from one
• Leave other empty
Leave other empty
– When one in use fills up
• Copy
Copy all directly (from stack) pointed to items from 
all directly (from stack) pointed to items from
current allocation space to currently empty space
• Copy all items pointed to by items in the new space 
(that space that was just empty) to the new space
(that space that was just empty) to the new space
– Allocate from this new (very recently empty) 
space
p
Garbage Collection
• Implicitly doing marking by moving (only 
move things not garbage)
– No requirements to tag everything garbage first 
(init‐phase)
– No requirements to reclaim after tracing (clean‐up 
phase)
– Benefits: No per‐object bloat for mark tag
• Costs: Two large blocks of memory reserved 
for each programs heap, one of which never 
really is used much at all for parts of the 
program; required to be able to change 
pointers
Heap Management
Heap Management
• Look at two different implementations:
p
– Heap as a group of fixed, single size cells
• More likely to be seen when language system is requesting from 
heap (LISP)
heap (LISP)
– Heap as a group of variable sized segments
• Required to support programmer requests (array of arbitrary size 
of contiguous memory) (Java C++)
of contiguous memory) (Java, C++)
Heaps: Single Sized Cells
Heaps: Single Sized Cells
• Define a cell as a unit that contains space for item of 
p
interest and a pointer
• Often implemented as a linked list of cells

Data
Goes
Here

• Available heap often called the “the free list”
l bl h f ll d h “ h f l ”
Heaps: Single Sized Cells
Heaps: Single Sized Cells

Free list before allocation Updated free list after allocation

• Allocation:
ll i
– Remove cell from front of free list
• De‐allocation:
– Attach released cells to the front of the free list
• No need for blocks/bytes in free list to be 
contiguous (no expected use case requiring
contiguous (no expected use case requiring 
contiguous data)
Heaps: Variable Sized Cells
Heaps: Variable Sized Cells
• More applicable to most programming languages’ 
needs
• General approach:
– Have “AvailableStart”  pointer initially point to a single 
cell that is sized to be all of available free heap memory
cell that is sized to be all of available free heap memory.
– Allocation: When a request is made: 
• If the cell at the front of the list is large enough, break the cell 
into two pieces one being the requested size and the other
into two pieces, one being the requested size and the other 
being everything else
• Deallocation: 
– Simple: Reclaimed, variable sized cells are added to the 
back of the list
Heaps: Variable Sized Cells
Heaps: Variable Sized Cells
• Below is results of allocates &deletes
Below is results of allocates &deletes

• Allocation:
• For a while, this technique will work fine
• If front cell isn’t large enough, what to do?
• If front cell isn’t large enough, try the next free block(s) 
on the list until find one that is large enough
on the list until find one that is large enough.
Heaps: Variable Sized Cells
Heaps: Variable Sized Cells
• This approach does entail list overhead:
–R
Requires searching through lists to check and see if there is 
i hi th h li t t h k d if th i
a block of appropriate size available
• Implementation Questions:
Implementation Questions:
– Do you
• Take
Take the first block that is big enough to handle the 
the first block that is big enough to handle the
request (“first‐fit”) or 
• Look for the “best fit” block, which could require 
looking at every block?
– Do you keep the list of different size blocks in 
sorted order by size?
– Search cost dependent on size of free list
Heaps: Variable Sized Cells
• This approach does entail list overhead:
– May hit a point where only have lots of small blocks sitting 
around
d
• Does this sound familiar? (CSC 241) fragmentation
• May over allocate by small chunks so try to reduce this issue, but 
this leads to internal waste
Heap: Variable Sized Cells
Heap: Variable Sized Cells
• Requires
Requires adjoining small blocks that were 
adjoining small blocks that were
from adjacent parts of memory back together 
(compaction)
– Partial compaction – search free‐space list for 
adjacent blocks, merge back together (can do this
adjacent blocks, merge back together (can do this 
when needed, or when inserting an item onto free 
space list)
– Full compaction – move all active heap entries to 
one end of heap memory, get large free block
• Requires ability to easily update pointers pointing to 
these moved blocks  (tombstones might help here?)
Garbage Collection:
Some Special Problems
• With fixed size cells (Lisp)
– EEvery cell is N bytes; sequential sweep, hopping by N to 
ll i N b t ti l h i b Nt
get to garbage bits
• With controlled languages (Lisp)
With controlled languages (Lisp)
– Can fix pointer location in cell (for tracing)
• With variable sized cells,
– In sequential scan, need to move up a variable amount 
of memory to find next heap entry to set garbage bit
• Need to store block size at start of block so can jump over block
Need to store block size at start of block so can jump over block
– Where to find the next pointer in the chain while 
tracing?
• R
Requires understanding of use of memory (types) or strict 
i d t di f f (t ) ti t
control of data layout (Java)

You might also like