28
A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management February 2, 2009

A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Embed Size (px)

Citation preview

Page 1: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

A Real-Time Garbage Collector Based on the Lifetimes of Objects

Henry Lieberman and Carl Hewitt

(CACM, June 1983)

Rudy Kaplan Depena

CS395T: Memory Management

February 2, 2009

Page 2: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Motivation: Internal Thinking

“Programs Do A Lot of Internal Thinking”- Lieberman and Hewitt

public void printAll(ArrayList list){Iterator it = new list.iterator();Object temp;while( it.hasNext() ){

temp = it.next();System.out.println( temp );

}}

Code snippet from Mike Scott’s lecture on Iterators[http://www.cs.utexas.edu/~scottm/cs307/handouts/Slides/Topic15Iterators.pdf]

Page 3: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Motivation: Reference Counting

Reference Counting claims young objects quickly

head Node Node Node

next nextnext

refCount = 2 refCount = 1 refCount = 1

Example from “Data Structures and Algorithms with Object-Oriented Design Patterns in Java”[http://www.brpreiss.com/books/opus5/html/page423.html]

Page 4: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Motivation: Ref Counting w/ Cycles

Houston We Have A Problem!

head Node Node Node

next nextnext

refCount = 1 refCount = 1 refCount = 1

Example from “Data Structures and Algorithms with Object-Oriented Design Patterns in Java”[http://www.brpreiss.com/books/opus5/html/page423.html]

Page 5: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Motivation: Cycles Are Needed

But Circular Structures are important programming technique for ….GraphicsCompilersOther Problem Spaces

So What Do We Do???

Page 6: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Real Time GC [Baker’s Algorithm]

QuickTime™ and a decompressor

are needed to see this picture.

Graphics from Lieberman and Hewitt[“A Real-Time Garbage Collector Based on the Lifetimes of Objects” - CACM June 1983]

Page 7: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

What’s Wrong With Baker?

Perhaps Too Slow?Total Garbage Collection Time?

Not Space Efficient?

Page 8: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Generational Hypothesis

“Recently created regions contain high % of garbage” while older regions do not

- Lieberman and HewittImprove Baker’s Algorithm

Optimize for scavenging short lived objectsScavenge long-lived objects less frequently

Rental CostStorage management cost of an object is proportional to the time during which the object is used

Inspired by Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

Page 9: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Modify Baker’s Algorithm

Address space broken up into generationsSmall set of pages (relative to address space)

Parallels between Baker Algo and Generational GCEvacuating <=> Condemning Regionfromspace <=> Obsolete Regiontospace <=> Non-Obsolete RegionEvacuating objects the same way

Inspired by Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

Page 10: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

In Action

1960.0

Regions are tagged with generation and version number

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

Page 11: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

In Action

1960.0

Allocation occurs in creation region

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

Page 12: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

In Action

1960.0

… until the creation region is full

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

Page 13: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

In Action

1960.0

… the new creation region is created

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

1970.0

• CONS composes existing components into objects• creates a backward pointer

Page 14: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

In Action

1960.0

… and so on

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

1970.0

Page 15: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

In Action

1960.0

… and so on

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

1970.0 1980.0

Page 16: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

In Action

1960.0

GC is initiated by condemning a region (making it obsolete) and creating an evacuation region

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/

LH83.pdf]

1970.0 1980.0

1970.1

Page 17: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

In Action

1960.0

This requires that younger generations also be condemnedWHY ?????

Modified from Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/

LH83.pdf]

1970.0 1980.0

1970.1 1980.1

Page 18: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

In Action

1960.0

Accessibile objects in condemned region(s) are incrementally evacuated using Baker’s Algorithm

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

1970.0 1980.0

1970.1 1980.1

Page 19: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

In Action

1960.0

… and the memory is recycled!

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

1970.1 1980.1

Page 20: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Varying GC Rates

Younger generations contain high percentages of garbage They should be collected frequently

Older generation contains relatively permanent data Older objects collected seldomly

Getting the most “Bang for your Buck” Scavange where there is high proportion of

inaccessible objects Minimize amount of scavenging needed per

inaccessible object Why is this important?

Modified from Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

Page 21: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Scavanging is Expensive!

We must restrict the storage that has to be scanned to find and update obsolete references

We restrict pointers from older generations to newer generations This means it will be faster to reclaim regions

in recent generations since little storage needs to be scavenged

What happens when an attempt is made to create a pointer from

older to younger generation????

Page 22: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Entry Tables

1960.0 1970.0 1980.0

In Lisp, RPLACA can assign a new pointer as a component to an older object

creates a forward pointer Add entry table to record forward pointers Adds a level of indirection for some pointers Won’t have to scan entire heap GC uses entry table entries as roots to the region

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

Page 23: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Weak PointersWeak pointers are …

Pointers in program which do not protect objects from being collected.Small in NumberUse entry tableWeak Pointers => Forward PointersForward Pointers > Weak PointersWeak Pointers are not persistently updated if object (i.e. - pointee) is garbage collected.Forward Pointers (which are NOT weak pointers) are persistently updated after GC occurs

Page 24: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Some Bad News

Fragmentation results from partially filled regions ….

How do we deal with this ???

Choose region sizes to minimize fragmentation Coalesce older regions reducing the amount of wasted space Perhaps use hybrid incremental collection sparingly

Page 25: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

An Optimization? -Value Cells & Stacks

Lisp implementations involve internal stacksHolds state info and variables

Stack must be scavenged for pointers to non-user-accessible objects Young objects lifetime correlated with pushing and popping the stack.

Suggests that good time to expect a lot of garbage when returning from function (remember printAll()?)

Page 26: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

An Optimization?-Flavors of new

Providing different allocators for different regions? Can users direct the flavor of allocation? Can compiler analysis change flavor of allocation? Can runtime information change flavor of allocation?

From Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

Page 27: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

Aspects of Program Behavior

Rate of object creation How fast are objects created?

Average lifetimes of objects How fast do objects become inaccessible

Proportion of forward vs backward pointers High % of pointing to old objects bodes well for this scheme

Average “length” of pointers What’s the locality like? Close locality would be nice :)

Modified from Maria Jump’s Presentation in Fall 2003 on Generational GC[http://www.cs.utexas.edu/users/mckinley/395Tmm-2003/talks/LH83.pdf]

Page 28: A Real-Time Garbage Collector Based on the Lifetimes of Objects Henry Lieberman and Carl Hewitt (CACM, June 1983) Rudy Kaplan Depena CS395T: Memory Management

System.out.println(“The End!”);