Title: Simple Generational GC
1Simple Generational GC
- Andrew W. Appel
- (Practice and Experience, February 1989)
Rudy Kaplan Depena CS 395T Memory
Management February 9, 2009
2Outline
- Motivation
- Idea Description
- Discussion
- Conclusion
- References
- The End
3Motivation Goals
- GC should be .
- Simple to implement
- Moon1 requires special hardware thats not
simple! - Fast to allocate
- Use idea that young objects point to old objects
- Not other way around thats not expensive!
- Low cost overhead
- Algo should be incremental (i.e. - amortize
collection cost)
4Motivation Mark-Sweep
2 From Vitaly Shmatikovs Presentation in
Spring 2008 on GC www.cs.utexas.edu/shmat/course
s/cs345_spring08/11garbage.ppt
5Motivation Mark-Sweep
2 From Vitaly Shmatikovs Presentation in
Spring 2008 on GC www.cs.utexas.edu/shmat/course
s/cs345_spring08/11garbage.ppt
6Motivation Mark-Sweep
2 From Vitaly Shmatikovs Presentation in
Spring 2008 on GC www.cs.utexas.edu/shmat/course
s/cs345_spring08/11garbage.ppt
7Motivation Mark-Sweep
2 From Vitaly Shmatikovs Presentation in
Spring 2008 on GC www.cs.utexas.edu/shmat/course
s/cs345_spring08/11garbage.ppt
8Motivation Mark-Sweep
- Good
- Incremental Garbage Reclamation
- Cost is amortized
- Bad
- Memory Size Proportional To Time
Memory Size
Time
9Motivation Copying
- Good
- Time Proportional to Number of Reachable Records
- No Free List
- Bad
- Can we be faster?
of Reachable Records
Time
10Idea Description Improvements
- Two Observations
- Pointers are acyclic
- Newer Records --gt Older Records
- Older Records --gt Newer Records
- a.k.a assignment
- Changing field in previously allocated record
- Generational Hypothesis
- Young objects more likely to become garbage soon
- Generational GC is a variant of Copying GC
- Records kept in regions of similar age
- GC copies reachable objects in An into new area
An w/o touching other areas
11Idea Description Assignment Table
- Lieberman Hewitts Entry Table
- Checking fetched values is done indirectly
- This is expensive
- When region, An, is collected, the whole
assignment tables must be examined - This is overhead
- Moon and Ungar provide different alternatives
- Generational GC not good for PLs that have
assignment as main mechanism for changing data
structures.
12Idea Description Copying Algo
- Heart of Generational GC
- Copy live data from one region (source region)
to another (destination region) - Forward Procedure
- Copies source to destination and returns pointer
- If previously copied, return copy w/o making new
copy - Scan pointer successively increments thru
destination region until scan catches up with next
133 From Mooly Sagivs Presentation on Memory
Management http//www.cs.tau.ac.il/msagiv/course
s/wcc08.html
143 From Mooly Sagivs Presentation on Memory
Management http//www.cs.tau.ac.il/msagiv/course
s/wcc08.html
153 From Mooly Sagivs Presentation on Memory
Management http//www.cs.tau.ac.il/msagiv/course
s/wcc08.html
163 From Mooly Sagivs Presentation on Memory
Management http//www.cs.tau.ac.il/msagiv/course
s/wcc08.html
173 From Mooly Sagivs Presentation on Memory
Management http//www.cs.tau.ac.il/msagiv/course
s/wcc08.html
18Idea Description Fast Allocation
- GC never touches garbage
- Unallocated memory always in contiguous region
(i.e. - no free list ) - (cons A B)
- Test free space pointer against free space limit
- If the limit is reached, call GC
- Subtract 2 from free space ptr
- Store A
- Store B
- Return current value of free-space pointer
19Idea Description Fast Allocation
0xFFFFFFFF
lt-- free space ptr
0x00000000
lt-- free space limit
20Idea Description Fast Allocation
0xFFFFFFFF
A
lt-- free space ptr
0x00000000
lt-- free space limit
21Idea Description Fast Allocation
0xFFFFFFFF
A
B
lt-- free space ptr
0x00000000
lt-- free space limit
22Idea Description Fast Allocation
- Use Virtual Memory hardware to make comparison
- 1. Test free-space ptr against free-space limit
- If we get a page fault from inaccessible page
mapped just before free space, we will initiate
garbage collection - Free space pointer kept in register
- Instructions to allocate A and B in (cons A B)
has little overhead on some platforms (e.g. - AIX)
23Idea Description Variable Sized Records
- Most PLs have various record sizes
- GC needs to know sizes in order to memory
management efficiently. Many ways to do this - Record can contain tag to explain format
- Records of different formats stored in different
areas of memory - PL with static type system (e.g.- Haskell, ML)
can provide GC with map of type system which
explains runtime format.
24Idea Description Arranging Generations
Unix program break
Page 8 on Appels Simple Generational Garbage
Collection and Fast Allocation
25Idea Description Arranging Generations
Page 9 on Appels Simple Generational Garbage
Collection and Fast Allocation
26Idea Description Arranging Generations
- If program uses at most A words of live data,
then size of memory M 2A - For Best performance, M gt 2A
- Invariant
- If A lt M/2, then GC never runs out of space
27Please Sir, May I Have Some More?
- ? ratio of memory size to live data
- ?o desired value of ?
- ? lt 2, algo will run out of space or degrade
performance - ? 3 for good performance
- Ask OS for more space under following
circumstances
28Please Sir, May I Have Some More?
- Circumstances for request of more memory
- After a major collection when ? lt ?o
- Collector runs out of space when trying to copy
older region to reserve region - After minor collection cycle, free region is not
much larger than requested by program.
29Idea Description Keeping Track of Assignments
- The GC uses the root set when it starts
collecting the newest area - It needs the root set to determine what is
reachable - We must maintain a list of every record assigned
into - This list can be maintained in software
30Idea Description Keeping Track of Assignments
- Upon instruction of form assign p to record r
- The compiler will insert add r to assignment
list
Assignment List
We May Exhaust free space failing the operation
31Idea Description Handling Page Traps
- When does a Page Trap Happen?
- When the mutator runs out of space
- The allocator tries to make a new record in an
inaccessible page, leading to a seg fault - Seg faults trigger GC
- Reset the free space pointer to appropriate
value, and return from interrupt
32Idea Description Handling Page Traps
- When GC is invoked, roots are
- Global Variables
- Runtime stack
- Machine registers
- Assignment list
- When data is moved from newer to older region,
these registers and placeholders get modified
33Discussion Space
- What if runtime system sampled ratio of live
data versus total memory over multiple program
runs and system consistently used a lot less
memory than was available? - Could we reduce space waste?
- What technologies would we be able to apply this
technique to? - What downfalls could this have?
34Discussion Security
- The root objects are the entry points for a
security breach . The collector would have to
access objects at various security levels this
would require the collector to be trusted - - Dieter Gollmann (Computer Security ESORICS
94) - Upon encounter of the assignment operation, we
place a record on the assignment linked list. - If there is an overflow, then more space for it
can be allocated or the garbage-collector may be
invoked - - Appel, Page 12
- Could there be security hole? DOS attack?
35Discussion Portability
- On a VAX the subtraction from the free-space
pointer can be implemented by means of an
auto-decrement addressing mode .. because
instructions take no more time than any other
pair of stores into memory, the overhead
attributable to an allocation from the heap is
exactly zero - Appel, 7 - Are these portability restrictions necessary, or
can we be more portable than this? Could we ever
use this type of GC in languages like Java given
these portability constraints?
36Discussion Portability
Would other stack layouts work for this scheme?
37Discussion Other Languages
- How well will this scheme perform on other
languages? - JavaScript
- C
- etc
38Conclusions
- Generational GC is not for every language and
can get expensive on assignments - GC becomes fast so overhead of creating object
is less than overhead to clean up mess - This paper attempts to present simple and
efficient arrangements of generational GC, while
gaining speed through special instructions and
other techniques
39References
- 1 David A. Moon, Garbage collection in a large
LISP system, ACM Symposium on LISP and
Functional Programming, pp 235-246, ACM, 1984 - 2 Vitaly Shmatikov, Garbage Collection, The
University of Texas at Austin, Spring 2008. - 3 Mooly Sagiv, Memory Management, Tel Aviv
University - 4 Dieter Gollmann, Computer Security ESORICS
94 Third European Symposium on Research in
Computer Security, Brighton, United Kingdom,
November 7-9, 1994 Proceedings