Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... ·...
Transcript of Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... ·...
![Page 1: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/1.jpg)
Garbage Collection
![Page 2: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/2.jpg)
Announcements
● Programming Project 4 due Saturday, August 18 at 11:30AM.● Stop by OH with questions!● Ask questions via email!● Ask questions via Piazza!
● Online course evaluation available on Axess.● Please give feedback!
● Programming Project 4 samples/ directory updated.● Sorry about that!
![Page 3: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/3.jpg)
Where We Are
Lexical Analysis
Semantic Analysis
Syntax Analysis
IR Optimization
IR Generation
Code Generation
Optimization
SourceCode
MachineCode
![Page 4: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/4.jpg)
Runtime Memory Management
● Most constructs in a programming language need memory.
● Some require a variable amount of memory:● Local variables● Objects● Arrays● Strings
● Some need a fixed amount of memory● (such as?)
![Page 5: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/5.jpg)
Memory Management So Far
● Some memory is preallocated and persists throughout the program:● Global variables, virtual function tables, executable
code, etc.
● Some memory is allocated on the runtime stack:● Local variables, parameters, temporaries.
● Some memory is allocated in the heap:● Arrays, objects.
● Memory management for the first two is trivial.● How do we manage heap-allocated memory?
![Page 6: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/6.jpg)
Manual Memory Management
● Option One: Have the programmer handle allocation and deallocation of dynamic memory.
● Approach used in C, C++.● Advantages:
● Programmer can exercise precise control over memory usage.
● Disadvantages:● Programmer has to exercise precise control
over memory usage.
![Page 7: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/7.jpg)
Strengths of Manual Management
● Comparatively easy to implement.● “Just” need a working memory manager.
● Allows programmers to make aggressive performance optimizations.● Programmer can choose allocation scheme
that achieves best performance.
![Page 8: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/8.jpg)
Problems with Manual Management
● Easily leads to troublesome bugs:● Memory leaks where resources are never
freed.● Double frees where a resource is freed
twice (major security risk).● Use-after-frees where a deallocated
resource is still used (major security risk).
● Programming languages with manual memory management are almost always not type-safe.
![Page 9: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/9.jpg)
Automatic Memory Management
● Idea: Have the runtime environment automatically reclaim memory.
● Objects that won't be used again are called garbage.
● Reclaiming garbage objects automatically is called garbage collection.
● Advantages:● Programmer doesn't have to reclaim unused resources.
● Disadvantages:● Programmer can't reclaim unused resources.
![Page 10: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/10.jpg)
Preliminaries
![Page 11: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/11.jpg)
What is Garbage?
● An object is called garbage at some point during execution if it will never be used again.
● What is garbage at the indicated points?
![Page 12: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/12.jpg)
What is Garbage?
● An object is called garbage at some point during execution if it will never be used again.
● What is garbage at the indicated points?
int main() { Object x, y; x = new Object(); y = new Object(); /* Point A */
x.doSomething(); y.doSomething(); /* Point B */
y = new Object(); /* Point C */}
![Page 13: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/13.jpg)
Approximating Garbage● In general, it is undecidable whether an object is
garbage.● Need to rely on a conservative approximation.
● An object is reachable if it can still be referenced by the program.● Goal for today: detect and reclaim unreachable objects.
● This does not prevent memory leaks!● Many reachable objects are never used again.● It is very easy to have memory leaks in garbage-
collected languages.● Interesting read: “Low-Overhead Memory Leak
Detection Using Adaptive Statistical Profiling” by Chilimbi and Hauswirth.
![Page 14: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/14.jpg)
Assumptions for Today
● Assume that, at runtime, we can find all existing references in the program.● Cannot fabricate a reference to an existing object ex
nihilo.● Cannot cast pointers to integers or vice-versa.
● Examples: Java, Python, JavaScript, PHP, etc.● Non-examples: C, C++● Advance knowledge of references allows for
precise introspection at runtime.
![Page 15: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/15.jpg)
Types of Garbage Collectors● Incremental vs stop-the-world:
● An incremental collector is one that runs concurrently with the program.
● A stop-the-world collector pauses program execution to look for garbage.
● Which is (generally) more precise?● Which would you use in a nuclear reactor control system?
● Compacting vs non-compacting:● A compacting collector is one that moves objects around
in memory.● A non-compacting collector is one that leaves all objects
where they originated.● Which (generally) spends more time garbage collecting?● Which (generally) leads to faster program execution?
![Page 16: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/16.jpg)
Reference Counting
![Page 17: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/17.jpg)
Reference Counting
● A simple framework for garbage collection.● Though it has several serious weaknesses!
● Idea: Store in each object a reference count (refcount) tracking how many references exist to the object.
● Creating a reference to an object increments its refcount.
● Removing a reference to an object decrements its refcount.
● When an object has zero refcount, it is unreachable and can be reclaimed.● This might decrease other objects' counts and trigger
more reclamations.
![Page 18: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/18.jpg)
Reference Counting in Action
![Page 19: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/19.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
![Page 20: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/20.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
![Page 21: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/21.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head
![Page 22: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/22.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 0
![Page 23: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/23.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 0
![Page 24: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/24.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
![Page 25: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/25.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
![Page 26: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/26.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
![Page 27: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/27.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
0
![Page 28: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/28.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
0
![Page 29: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/29.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1
![Page 30: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/30.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1
![Page 31: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/31.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
![Page 32: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/32.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
0
![Page 33: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/33.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
0
![Page 34: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/34.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
1
![Page 35: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/35.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
1
![Page 36: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/36.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
1
![Page 37: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/37.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
2tail
1
![Page 38: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/38.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
2tail
1
![Page 39: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/39.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
2tail
1
![Page 40: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/40.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
2tail
2
![Page 41: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/41.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
2tail
2
![Page 42: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/42.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
2tail
2
![Page 43: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/43.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
2tail
1
![Page 44: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/44.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
2tail
1
![Page 45: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/45.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
1
![Page 46: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/46.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
1
![Page 47: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/47.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
1
![Page 48: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/48.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
0
![Page 49: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/49.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
0Reclaimed!
![Page 50: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/50.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
![Page 51: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/51.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
![Page 52: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/52.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 1
mid
1tail
![Page 53: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/53.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 0
mid
1tail
![Page 54: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/54.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head 0
mid
1tail
Reclaimed!
![Page 55: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/55.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head
mid
1tail
![Page 56: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/56.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head
mid
0tail
![Page 57: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/57.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head
mid
0tailReclaimed!
![Page 58: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/58.jpg)
Reference Counting in Actionclass LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; mid = tail = null;
head.next.next = null;
head = null;}
head
mid
tail
![Page 59: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/59.jpg)
Reference Counting Details
● When creating an object, set its refcountto 0.
● When creating a reference to an object, increment its refcount.
● When removing a reference from an object:● Decrement its refcount.● If its refcount is zero:
– Remove all outgoing references from that object.– Reclaim the memory for that object.
![Page 60: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/60.jpg)
One Major Problem
![Page 61: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/61.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
![Page 62: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/62.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
![Page 63: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/63.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
1tail
1
![Page 64: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/64.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
1tail
1
![Page 65: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/65.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
1tail
1
![Page 66: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/66.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
2tail
1
![Page 67: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/67.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
2tail
1
![Page 68: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/68.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
2tail
1
![Page 69: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/69.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
2tail
2
![Page 70: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/70.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
2tail
2
![Page 71: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/71.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
2tail
2
![Page 72: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/72.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 2
mid
2tail
2
![Page 73: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/73.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 2
mid
2tail
2
![Page 74: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/74.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 2
mid
2tail
2
![Page 75: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/75.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
2tail
2
![Page 76: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/76.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
2tail
2
![Page 77: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/77.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
2tail
2
![Page 78: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/78.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
1tail
2
![Page 79: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/79.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
1tail
2
![Page 80: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/80.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
1tail
2
![Page 81: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/81.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
1tail
1
![Page 82: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/82.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
1tail
1
![Page 83: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/83.jpg)
One Major Problem
class LinkedList { LinkedList next;}
int main() { LinkedList head = new LinkedList; LinkedList mid = new LinkedList; LinkedList tail = new LinkedList;
head.next = mid; mid.next = tail; tail.next = head;
head = null; mid = null; tail = null;}
head 1
mid
1tail
1Problem?
![Page 84: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/84.jpg)
Reference Cycles
● A reference cycle is a set of objects that cyclically refer to one another.
● Because all the objects are referenced, all have nonzero refcounts and are never reclaimed.
● Issue: Refcount tracks number of references, not number of reachable references.
● Major problems in languages/systems that use reference counting:● e.g. Perl, Firefox 2.
![Page 85: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/85.jpg)
Analysis of Reference Counting
● Advantages:● Simple to implement.● Can be implemented as a library on top of
explicit memory management (see C++ shared_ptr).
● Disadvantages:● Fails to reclaim all unreachable objects.● Can be slow if a large collection is initiated.● Noticeably slows down assignments.
![Page 86: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/86.jpg)
Mark-and-Sweep
![Page 87: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/87.jpg)
Reachability Revisited
● Recall that the goal of our garbage collector is to reclaim all unreachable objects.
● Reference counting tries to find unreachable objects by finding objects with no incoming references.
● Imprecise because we forget which references those are.
![Page 88: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/88.jpg)
Mark-and-Sweep: The Intuition
● Intuition: Given knowledge of what's immediately accessible, find everything reachable in the program.
● The root set is the set of memory locations in the program that are known to be reachable.● (Such as?)
● Any objects reachable from the root set are reachable.● Any objects not reachable from the root set are not
reachable.● Do a graph search starting at the root set!
![Page 89: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/89.jpg)
Mark-and-Sweep: The Algorithm
● Mark-and-sweep runs in two phases.● Marking phase: Find reachable objects.
● Add the root set to a worklist.● While the worklist isn't empty:
– Remove an object from the worklist.– If it is not marked, mark it and add to the worklist all objects
reachable from that object.
● Sweeping phase: Reclaim free memory.● For each allocated object:
– If that object isn't marked, reclaim its memory.– If the object is marked, unmark it (so on the next mark-and-
sweep iteration we have to mark it again).
![Page 90: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/90.jpg)
Mark-and-Sweep In Action
![Page 91: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/91.jpg)
Mark-and-Sweep In Action
![Page 92: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/92.jpg)
Mark-and-Sweep In Action
Root Set
![Page 93: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/93.jpg)
Mark-and-Sweep In Action
Root Set
Work List
![Page 94: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/94.jpg)
Mark-and-Sweep In Action
Root Set
Work List
![Page 95: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/95.jpg)
Mark-and-Sweep In Action
Root Set
Work List
![Page 96: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/96.jpg)
Mark-and-Sweep In Action
Root Set
Work List
mark
![Page 97: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/97.jpg)
Mark-and-Sweep In Action
mark
Root Set
Work List
![Page 98: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/98.jpg)
Mark-and-Sweep In Action
mark
Root Set
Work List
![Page 99: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/99.jpg)
Mark-and-Sweep In Action
mark
Root Set
Work List
![Page 100: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/100.jpg)
Mark-and-Sweep In Action
mark
Root Set
Work List
mark
![Page 101: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/101.jpg)
Mark-and-Sweep In Action
mark
markRoot Set
Work List
![Page 102: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/102.jpg)
Mark-and-Sweep In Action
mark
markRoot Set
Work List
![Page 103: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/103.jpg)
Mark-and-Sweep In Action
mark
markRoot Set
Work List
![Page 104: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/104.jpg)
Mark-and-Sweep In Action
mark
markRoot Set
Work List
mark
![Page 105: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/105.jpg)
Mark-and-Sweep In Action
mark
markRoot Set
Work List
mark
![Page 106: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/106.jpg)
Mark-and-Sweep In Action
mark
markRoot Set
mark
Work List
![Page 107: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/107.jpg)
Mark-and-Sweep In Action
mark
markRoot Set
mark
Work List
![Page 108: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/108.jpg)
Mark-and-Sweep In Action
mark
markRoot Set
mark
Work List
![Page 109: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/109.jpg)
Mark-and-Sweep In Action
mark
markRoot Set
mark
Work List
mark
![Page 110: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/110.jpg)
Mark-and-Sweep In Action
mark
markRoot Set
mark
Work List
mark
![Page 111: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/111.jpg)
Mark-and-Sweep In Action
mark
mark
mark
Root Set
mark
Work List
![Page 112: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/112.jpg)
Mark-and-Sweep In Action
mark
mark
mark
Root Set
mark
Work List
![Page 113: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/113.jpg)
Mark-and-Sweep In Action
mark
mark
mark
Root Set
mark
Work List
![Page 114: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/114.jpg)
Mark-and-Sweep In Action
mark
mark
mark
Root Set
mark
Work List
mark
![Page 115: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/115.jpg)
Mark-and-Sweep In Action
mark
mark
mark
Root Set
mark
Work List
mark
![Page 116: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/116.jpg)
Mark-and-Sweep In Action
mark
mark
mark
Root Set
mark
mark
Work List
![Page 117: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/117.jpg)
Mark-and-Sweep In Action
mark
mark
mark
Root Set
mark
mark
Work List
![Page 118: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/118.jpg)
Mark-and-Sweep In Action
mark
mark
mark
Root Set
mark
mark
Work List
![Page 119: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/119.jpg)
Mark-and-Sweep In Action
mark
mark
mark
Root Set
mark
mark
Work List
Reclaimed!
Reclaimed!
![Page 120: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/120.jpg)
Mark-and-Sweep In Action
mark
mark
mark
Root Set
mark
mark
Work List
![Page 121: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/121.jpg)
Mark-and-Sweep In Action
Root Set
Work List
![Page 122: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/122.jpg)
Mark-and-Sweep In Action
![Page 123: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/123.jpg)
Implementing Mark-and-Sweep
● The mark-and-sweep algorithm, as described, has two serious problems.
● Runtime proportional to number of allocated objects.● Sweep phase visits all objects to free them or
clear marks.
● Work list requires lots of memory.● Amount of space required could potentially be
as large as all of memory.● Can't preallocate this space!
![Page 124: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/124.jpg)
The Key Idea
● During a mark-and-sweep collection, every allocated block must be in exactly one of four states:● Marked: This object is known to be reachable.● Enqueued: This object is in the worklist.● Unknown: This object has not yet been seen.● Deallocated: This object has already been freed.
● Augment every allocated block with two bits to encode which of these four states the object is in.
● Maintain doubly-linked lists of all the objects in each of these states.
![Page 125: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/125.jpg)
Baker's Algorithm● Move all of the root set to the enqueued list.● While the enqueued list is not empty:
● Move the first object from the enqueued list to the marked list.
● For each unknown object referenced, add it to the enqueued list.
● At this point, everything reachable is in marked and everything unreachable is in unknown.
● Concatenate the unknown and deallocated lists● Deallocates all garbage in O(1).
● Move everything from the marked list to the unknown list.● Can be done in O(1).● Indicates objects again must be proven reachable on next scan.
![Page 126: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/126.jpg)
One Last Detail
● But wait – if we're already out of memory, how do we build these linked lists?
● Idea: Since every object can only be in one linked list, embed the next and previous pointers into each allocated block.
![Page 127: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/127.jpg)
One Last Detail
● But wait – if we're already out of memory, how do we build these linked lists?
● Idea: Since every object can only be in one linked list, embed the next and previous pointers into each allocated block.
ObjectFields
Vtable *
Previous
Next
ObjectFields
Vtable *
Previous
Next
ObjectFields
Vtable *
Previous
Next
State State State
![Page 128: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/128.jpg)
Analysis of Mark-and-Sweep
● Advantages:● Precisely finds exactly the reachable objects.● Using Baker's algorithm, runs in time
proportional to the number of reachable objects.
● Disadvantages:● Stop-the-world approach may introduce huge
pause times.● Linked list / state information in each allocated
block uses lots of memory per object.
![Page 129: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/129.jpg)
Stop-and-Copy
![Page 130: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/130.jpg)
Improving Performance
● There are many ways to improve a program's performance, some of which can be improved by a good garbage collector:
● Increasing locality.● Memory caches are often designed to hold adjacent memory
locations.● Placing objects consecutively in memory can improve
performance by reducing cache misses.
● Increasing allocation speed.● Many languages (Java, Haskell, Python, etc.) allocate objects
frequently.● Speeding up object allocation can speed up program
execution.
![Page 131: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/131.jpg)
Increasing Locality
● Idea: When doing garbage collection, move all objects in memory so that they are adjacent to one another.● This is called compaction.
● Ideally, move objects that reference one another into adjacent memory locations.
● Garbage collector must update all pointers in all objects to refer to the new object locations.
● Could you do this in Java?● Could you do this in C++?
![Page 132: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/132.jpg)
Increasing Allocation Speed
● Typically implementations of malloc and free use free lists, linked lists of free memory blocks.
● Allocating an object requires following these pointers until a suitable object is found.● Usually fast, but at least 10 – 20 assembly
instructions.● Contrast with stack allocation – just one assembly
instruction!● Can we somehow get the performance speed of
the stack for dynamic allocation?
![Page 133: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/133.jpg)
The Stop-and-Copy Collector
![Page 134: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/134.jpg)
The Stop-and-Copy Collector
![Page 135: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/135.jpg)
The Stop-and-Copy Collector
All of memory
![Page 136: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/136.jpg)
The Stop-and-Copy Collector
![Page 137: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/137.jpg)
The Stop-and-Copy Collector
![Page 138: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/138.jpg)
The Stop-and-Copy Collector
New Space Old Space
![Page 139: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/139.jpg)
The Stop-and-Copy Collector
![Page 140: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/140.jpg)
The Stop-and-Copy Collector
Free Space
![Page 141: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/141.jpg)
The Stop-and-Copy Collector
Free Space
![Page 142: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/142.jpg)
The Stop-and-Copy Collector
Free Space
![Page 143: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/143.jpg)
The Stop-and-Copy Collector
Free Space
![Page 144: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/144.jpg)
The Stop-and-Copy Collector
Free Space
![Page 145: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/145.jpg)
The Stop-and-Copy Collector
Free Space
![Page 146: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/146.jpg)
The Stop-and-Copy Collector
Free Space
![Page 147: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/147.jpg)
The Stop-and-Copy Collector
Free Space
![Page 148: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/148.jpg)
The Stop-and-Copy Collector
Free Space
![Page 149: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/149.jpg)
The Stop-and-Copy Collector
Free Space
![Page 150: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/150.jpg)
The Stop-and-Copy Collector
Free Space
![Page 151: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/151.jpg)
The Stop-and-Copy Collector
Free Space
Out of space!
![Page 152: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/152.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 153: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/153.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 154: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/154.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 155: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/155.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 156: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/156.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 157: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/157.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 158: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/158.jpg)
The Stop-and-Copy Collector
Root Set
Free Space
![Page 159: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/159.jpg)
The Stop-and-Copy Collector
Root Set
Free Space
![Page 160: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/160.jpg)
The Stop-and-Copy Collector
Root Set
Free Space
![Page 161: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/161.jpg)
The Stop-and-Copy Collector
Free Space
![Page 162: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/162.jpg)
The Stop-and-Copy Collector
Free Space
![Page 163: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/163.jpg)
The Stop-and-Copy Collector
Free Space
![Page 164: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/164.jpg)
The Stop-and-Copy Collector
Free Space
![Page 165: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/165.jpg)
The Stop-and-Copy Collector
Free Space
![Page 166: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/166.jpg)
The Stop-and-Copy Collector
Free Space
![Page 167: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/167.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 168: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/168.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 169: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/169.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 170: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/170.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 171: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/171.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 172: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/172.jpg)
The Stop-and-Copy Collector
Free Space
Root Set
![Page 173: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/173.jpg)
The Stop-and-Copy Collector
Root Set
Free Space
![Page 174: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/174.jpg)
The Stop-and-Copy Collector
Root Set
Free Space
![Page 175: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/175.jpg)
The Stop-and-Copy Collector
Root Set
Free Space
![Page 176: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/176.jpg)
The Stop-and-Copy Collector
Free Space
![Page 177: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/177.jpg)
Stop-and-Copy in Detail● Partition memory into two regions: the old space and the
new space.● Keep track of the next free address in the new space.● To allocate n bytes of memory:
● If n bytes space exist at the free space pointer, use those bytes and advance the pointer.
● Otherwise, do a copy step.
● To execute a copy step:● For each object in the root set:
– Copy that object over to the start of the old space.– Recursively copy over all objects reachable from that object.
● Adjust the pointers in the old space and root set to point to new locations.
● Exchange the roles of the old and new spaces.
![Page 178: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/178.jpg)
Implementing Stop and Copy
● The only tricky part about stop-and-copy is adjusting the pointers in the copied objects correctly.
● Idea: Have each object contain a extra space for a forwarding pointer.
● To clone an object:● First, do a complete bitwise copy of the object.
– All pointers still point to their original locations.● Next, set the forwarding pointer of the original object to
point to the new object.
● Finally, after cloning each object, for each pointer:● Follow the pointer to the object it references.● Replace the pointer with the pointee's forwarding pointer.
![Page 179: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/179.jpg)
Forwarding Pointers
![Page 180: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/180.jpg)
Forwarding Pointers
Root Set
![Page 181: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/181.jpg)
Forwarding Pointers
Root Set
![Page 182: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/182.jpg)
Forwarding Pointers
Root Set
![Page 183: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/183.jpg)
Forwarding Pointers
Root Set
![Page 184: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/184.jpg)
Forwarding Pointers
Root Set
![Page 185: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/185.jpg)
Forwarding Pointers
Root Set
![Page 186: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/186.jpg)
Forwarding Pointers
Root Set
![Page 187: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/187.jpg)
Forwarding Pointers
Root Set
![Page 188: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/188.jpg)
Forwarding Pointers
Root Set
![Page 189: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/189.jpg)
Forwarding Pointers
Root Set
![Page 190: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/190.jpg)
Forwarding Pointers
Root Set
![Page 191: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/191.jpg)
Analysis of Stop-and-Copy
● Advantages:● Implementation simplicity (compared to mark-and-
sweep).● Fast memory allocation; using OS-level tricks, can
allocate in a single assembly instruction.● Excellent locality; depth-first ordering of copied
objects places similar objects near each other.
● Disadvantages:● Requires half of memory to be free at all times.● Collection time proportional to number of bytes
used by objects.
![Page 192: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/192.jpg)
Hybrid Approaches
![Page 193: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/193.jpg)
The Best of All Worlds
● The best garbage collectors in use today are based on a combination of smaller garbage collectors.
● Each garbage collector is targeted to reclaim specific types of garbage.
● Usually has some final “fallback” garbage collector to handle everything else.
![Page 194: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/194.jpg)
Objects Die Young
● The Motto of Garbage Collection: Objects Die Young.
● Most objects have extremely short lifetimes.● Objects allocated locally in a function.● Temporary objects used to construct larger
objects.
● Optimize garbage collection to reclaim young objects rapidly while spending less time on older objects.
![Page 195: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/195.jpg)
Generational Garbage Collection● Partition memory into several “generations.”● Objects are always allocated in the first
generation.● When the first generation fills up, garbage collect
it.● Runs quickly; collects only a small region of memory.
● Move objects that survive in the first generation long enough into the next generation.
● When no space can be found, run a full (slower) garbage collection on all of memory.
![Page 196: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/196.jpg)
Garbage Collection in Java
![Page 197: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/197.jpg)
Garbage Collection in JavaEden
![Page 198: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/198.jpg)
Garbage Collection in JavaEden
Survivor Objects
![Page 199: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/199.jpg)
Garbage Collection in JavaEden
Tenured Objects
Survivor ObjectsSurvivor ObjectsSurvivor Objects
![Page 200: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/200.jpg)
Garbage Collection in JavaEden
Tenured Objects
Survivor Objects
![Page 201: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/201.jpg)
Garbage Collection in JavaEden
Tenured Objects
Survivor Objects
![Page 202: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/202.jpg)
Garbage Collection in JavaEden
Tenured Objects
Survivor Objects
![Page 203: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/203.jpg)
Garbage Collection in JavaEden
Tenured Objects
Survivor Objects
![Page 204: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/204.jpg)
Garbage Collection in JavaEden
Tenured Objects
Survivor Objects
![Page 205: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/205.jpg)
Garbage Collection in JavaEden
Tenured Objects
Survivor Objects
![Page 206: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/206.jpg)
Garbage Collection in JavaEden
0
Tenured Objects
Survivor Objects
![Page 207: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/207.jpg)
Garbage Collection in JavaEden
0
Tenured Objects
Survivor Objects
![Page 208: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/208.jpg)
Garbage Collection in JavaEden
0
Tenured Objects
Survivor Objects
![Page 209: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/209.jpg)
Garbage Collection in JavaEden
0
Tenured Objects
Survivor Objects
![Page 210: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/210.jpg)
Garbage Collection in JavaEden
0
Tenured Objects
Survivor Objects
![Page 211: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/211.jpg)
Garbage Collection in JavaEden
0
Tenured Objects
Survivor Objects
![Page 212: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/212.jpg)
Garbage Collection in JavaEden
0
Tenured Objects
Survivor Objects
![Page 213: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/213.jpg)
Garbage Collection in JavaEden
0
Tenured Objects
Survivor Objects
![Page 214: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/214.jpg)
Garbage Collection in JavaEden
0 0
Tenured Objects
Survivor Objects
![Page 215: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/215.jpg)
Garbage Collection in JavaEden
0 0
Tenured Objects
Survivor Objects
![Page 216: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/216.jpg)
Garbage Collection in JavaEden
0 0
Tenured Objects
Survivor Objects
![Page 217: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/217.jpg)
Garbage Collection in JavaEden
0 0
Tenured Objects
Survivor Objects
![Page 218: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/218.jpg)
Garbage Collection in JavaEden
0 0
Tenured Objects
Survivor Objects
![Page 219: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/219.jpg)
Garbage Collection in JavaEden
0 0
Tenured Objects
Survivor Objects
![Page 220: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/220.jpg)
Garbage Collection in JavaEden
0 0
Tenured Objects
Survivor Objects
![Page 221: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/221.jpg)
Garbage Collection in JavaEden
0 0
Tenured Objects
Survivor Objects
![Page 222: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/222.jpg)
Garbage Collection in JavaEden
0 0
Tenured Objects
Survivor Objects
![Page 223: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/223.jpg)
Garbage Collection in JavaEden
0 0
Tenured Objects
Survivor Objects
![Page 224: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/224.jpg)
Garbage Collection in JavaEden
10
Tenured Objects
Survivor Objects
![Page 225: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/225.jpg)
Garbage Collection in JavaEden
1
Tenured Objects
Survivor Objects
![Page 226: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/226.jpg)
Garbage Collection in JavaEden
1 0
Tenured Objects
Survivor Objects
![Page 227: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/227.jpg)
Garbage Collection in JavaEden
1 0
Tenured Objects
Survivor Objects
![Page 228: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/228.jpg)
Garbage Collection in JavaEden
1 0
Tenured Objects
Survivor Objects
![Page 229: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/229.jpg)
Garbage Collection in JavaEden
1 0
Tenured Objects
Survivor Objects
![Page 230: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/230.jpg)
Garbage Collection in JavaEden
1 0
Tenured Objects
Survivor Objects
![Page 231: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/231.jpg)
Garbage Collection in JavaEden
1 0
Tenured Objects
Survivor Objects
![Page 232: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/232.jpg)
Garbage Collection in JavaEden
1 0
Tenured Objects
Survivor Objects
![Page 233: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/233.jpg)
Garbage Collection in JavaEden
1 0
Tenured Objects
Survivor Objects
![Page 234: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/234.jpg)
Garbage Collection in JavaEden
1 0
Tenured Objects
Survivor Objects
![Page 235: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/235.jpg)
Garbage Collection in JavaEden
0
Tenured Objects
Survivor Objects
![Page 236: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/236.jpg)
Garbage Collection in JavaEden
1
Tenured Objects
Survivor Objects
![Page 237: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/237.jpg)
Garbage Collection in JavaEden
01
Tenured Objects
Survivor Objects
![Page 238: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/238.jpg)
Garbage Collection in JavaEden
01
Tenured Objects
Survivor Objects
![Page 239: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/239.jpg)
HotSpot Garbage Collection
● New objects are allocated using a modified stop-and-copy collector in the Eden space.
● When Eden runs out of space, the stop-and-copy collector moves its elements to the survivor space.
● Objects that survive long enough in the survivor space become tenured and are moved to the tenured space.
● When memory fills up, a full garbage collection (perhaps mark-and-sweep) is used to garbage-collect the tenured objects.
![Page 240: Garbage Collection - Stanford Universityweb.stanford.edu/class/archive/cs/cs143/cs143.1128/... · 2012-08-13 · Reference Counting A simple framework for garbage collection. Though](https://reader035.fdocuments.us/reader035/viewer/2022063004/5f8cfb24c8dbd708ba316d4e/html5/thumbnails/240.jpg)
Next Time
● Final Code Optimization● Instruction scheduling.● Locality optimizations.
● Where to Go From Here● Final Thoughts