University ¡of ¡Washington ¡
Memory ¡Alloca6on ¡III ¡
The ¡Hardware/So>ware ¡Interface ¡
CSE351 ¡Winter ¡2013 ¡
University ¡of ¡Washington ¡
Implicit ¡Memory ¡Alloca6on: ¡ Garbage ¡Collec6on ¡
Garbage ¡collec+on: ¡automa6c ¡reclama6on ¡of ¡heap-‑allocated ¡
storage—applica6on ¡never ¡has ¡to ¡free ¡
Common ¡in ¡func6onal ¡languages, ¡scrip6ng ¡languages, ¡and ¡
modern ¡object ¡oriented ¡languages: ¡
- Lisp, ¡ML, ¡Java, ¡Perl, ¡Mathema<ca ¡
Variants ¡(“conserva6ve” ¡garbage ¡collectors) ¡exist ¡for ¡C ¡and ¡C++ ¡
- However, ¡cannot ¡necessarily ¡collect ¡all ¡garbage ¡
¡
void foo() { int *p = (int *)malloc(128); return; /* p block is now garbage */ } 2 ¡
Winter ¡2013 ¡ Memory ¡Alloca6on ¡III ¡
University ¡of ¡Washington ¡
Garbage ¡Collec6on ¡
How ¡does ¡the ¡memory ¡allocator ¡know ¡when ¡memory ¡can ¡be ¡
freed? ¡
- In ¡general, ¡we ¡cannot ¡know ¡what ¡is ¡going ¡to ¡be ¡used ¡in ¡the ¡future ¡since ¡it ¡
depends ¡on ¡condi<onals ¡
- But, ¡we ¡can ¡tell ¡that ¡certain ¡blocks ¡cannot ¡be ¡used ¡if ¡there ¡are ¡no ¡
pointers ¡to ¡them ¡
So ¡the ¡memory ¡allocator ¡needs ¡to ¡know ¡what ¡is ¡a ¡pointer ¡and ¡
what ¡is ¡not ¡– ¡how ¡can ¡it ¡do ¡this? ¡ ¡
We’ll ¡make ¡some ¡assump6ons ¡about ¡pointers: ¡
- Memory ¡allocator ¡can ¡dis<nguish ¡pointers ¡from ¡non-‑pointers ¡
- All ¡pointers ¡point ¡to ¡the ¡start ¡of ¡a ¡block ¡in ¡the ¡heap ¡
- Applica<on ¡cannot ¡hide ¡pointers ¡ ¡
(e.g., ¡by ¡coercing ¡them ¡to ¡an ¡int, ¡and ¡then ¡back ¡again) ¡
3 ¡
Winter ¡2013 ¡ Memory ¡Alloca6on ¡III ¡
University ¡of ¡Washington ¡
Classical ¡GC ¡Algorithms ¡
Mark-‑and-‑sweep ¡collec6on ¡(McCarthy, ¡1960) ¡
- Does ¡not ¡move ¡blocks ¡(unless ¡you ¡also ¡“compact”) ¡
Reference ¡coun6ng ¡(Collins, ¡1960) ¡
- Does ¡not ¡move ¡blocks ¡(not ¡discussed) ¡
Copying ¡collec6on ¡(Minsky, ¡1963) ¡
- Moves ¡blocks ¡(not ¡discussed) ¡
Genera6onal ¡Collectors ¡(Lieberman ¡and ¡Hewi\, ¡1983) ¡
- Collec<on ¡based ¡on ¡life<mes ¡
- Most ¡alloca<ons ¡become ¡garbage ¡very ¡soon ¡
- So ¡focus ¡reclama<on ¡work ¡on ¡zones ¡of ¡memory ¡recently ¡allocated ¡
For ¡more ¡informa6on: ¡ ¡
Jones ¡and ¡Lin, ¡“Garbage ¡Collec+on: ¡Algorithms ¡for ¡Automa+c ¡ Dynamic ¡Memory”, ¡John ¡Wiley ¡& ¡Sons, ¡1996. ¡
4 ¡
Winter ¡2013 ¡ Memory ¡Alloca6on ¡III ¡