Top Banner
Uniprocessor Garbage Collection Techniques Paul R. Wilson
45

Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Uniprocessor Garbage Collection Techniques

Paul R. Wilson

Page 2: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

FromSapce/Tospace before

Page 3: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Ft after

Page 4: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Too much

Page 5: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

3mb vs. 6mb

Page 6: Uniprocessor Garbage Collection Techniques Paul R. Wilson.
Page 7: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

The Two-Phase Abstraction 1. Detection

2. Reclamation

Page 8: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Why Garbage Collect at All? Safety

Memory leaks Continued use of freed pointers

Simplicity

Page 9: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Why Garbage Collect at All? Flexibility

Hard coded program limits Efficiency!

Who is responsible for deletion? Extraneous copies

Page 10: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Liveness and Garbage There is a root set which is defined

as live.

Anything reachable from a live pointer is also live

Everything else is garbage

Page 11: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

The Root Set The Root Set

Static global and module variables Local Variables Variables on any activation stack(s)

Everyone else Anything Reachable From a live value

Page 12: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Reference Counting Advantages

Implicitly distributes garbage collection

Real Time guarantees with deferred reclamation Keep a list of zeroed objects not yet

processed Memory efficiency, can utilize all

available memory with no work room

Page 13: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Reference Counting Pitfalls Conservative- needs a separate GC

technique to reclaim cycles Expensive- pointer reassignment

requires: Increment Decrement Zero Check

Stack Variables frequent creation/destruction Can be optimized to some extent

Page 14: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Reference Counting

Page 15: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Ref counting, unreclaimable

Page 16: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Deferred Reference Counting Defer deletion of zero counted

objects Periodically scan the stack for

pointers

Page 17: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Mark-Sweep Collection Starting From the root set traverse

all pointers via depth/breadth first search.

Free everything that is not marked.

Page 18: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Non-Copying issues Same as for traditional allocators

Fragmentation Memory block size management Locality of reference- interleaved new/old

General issues- work proportional to heap size

Page 19: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Copying Advantages Memory locality preserved

Disadvantages Lots of copying!

“Scavenging”

Page 20: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Stop and Copy How to update multiple pointers to

the same object? Forwarding Pointers

Mark/Sweep is proportional to the amount of live data. Assuming this stays roughly constant, increasing memeory will increase efficiency.

Page 21: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Non Copying Version Facts

Allocated with a color Fragmentation

Advantages Does not require pointer rewriting

Supports obscure pointer formats, C friendly

Page 22: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

In place collection Conservative estimates

Useful for languages like C Pointers can be safely passed to

foreign libraries not written with Garbage Collection in mind

Page 23: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Incremental Tracing Collectors The ‘Mutator’ The reachability graph may change

From the garbage collectors point of view the actual application is merely a coroutine ir cuncurrent process with an unfortunate tendency to modify data structures that the collector is trying to traverse

Floating Garbage Can’t survive more than one extra round

Page 24: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Real Time Garbage Collection Incremental Tracing Collectors

In Place Collection Many readers single writer(mutator)

As a Copying Collector Multiple Readers Multiple Writers

Page 25: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Tricolor Marking White

Initial color for an object subject to collection

Black Objects that will be retained after the current

round

gray Object has been reached, but not its descendents

Wave front effect

Page 26: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

A violation of the Coloring Invariant

Page 27: Uniprocessor Garbage Collection Techniques Paul R. Wilson.
Page 28: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Read Barrier Detects an attempt to read a white

object and immediately colors it gray

Page 29: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Write Barrier Traps attempts to write a pointer

into an object

Page 30: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Some algorithms Snapshot-at-beginning write

barrier Black-only read barrier Baker’s read barrier Dijkstra’s write Barrier Steele’s write Barrier

Page 31: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Baker’s Read Barrier Allocates Black Grey Objects cannot be reverted to

white Immediately Invalidates fromspace Any pointer access to fromspace

causes the GC to grey the target object by copying it to tospace if necessary and updating the pointer.

Page 32: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Baker’s Non Copying Scheme Real Time Friendly

Page 33: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Treadmill

Page 34: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Black Only Read Barrier When a white object in fromspace

is touched it is scanned completely.

Page 35: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Replication Copying Collection Until copying from from space to to

space is completed, the mutator continues to read from from space.

Write updates must be trapped to update tospace.

Single simultaneous ‘flip’ where all pointers are updated.

Expensive for standard hardware, but cheap for functional languages

Page 36: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Real time considerations Read Barriers add an unpredictable cost

per pointer access Nilson background scavenger, reserve only

Write barrier may be more expensive overall, but the cost per access is well bounded

Guaranteeing progress allocation clock, frees per allocation

Statically allocate troublesome objects

Page 37: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Results Writer barrier more efficient on

standard hardware

Page 38: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Snapshot at the Beginning Catches pointers which try to escape from white

objects If a pointer is replace in a black object, the

replaced pointer is first stored. All overwritten pointers are saved via a write barrier. All objects that are live at the beginning of collection

remain live Allocate Black during collection round

Incremental Update Reverts black to gray when an object is written to, or

else grays they new pointed to object

Page 39: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Incremental Update with Write-Barrier(Dijkstra)g

Catches pointers that try to hide in black objects Reverts Black to gray If the overwritten pointer is not

pointed to elsewhere then it is garbage

Allocated white. Newly allocated objects assumed unreachable

Page 40: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Motivation for a new Strategy Most objects are short lived

80% to 90% die within a few million instructions

Objects that don’t die quickly are more likely to live a while

Long lived objects are copied over and over

Excessive Paging in Scanning if the heap must exceed available physical memory

Page 41: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Generational Garbage Collection

Page 42: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Generational gc before

Page 43: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Generational gc after

Page 44: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Gc memory usage

Page 45: Uniprocessor Garbage Collection Techniques Paul R. Wilson.

Variations of generational collection Intergenerational references

Write barrier Old to younger Young to old

Collection Advancement policies

Advance always Advance after 2 roundsCounter in the header field?Advance always? Semispace in the last generation3 spacesBucket brigadeMark compact in the oldest generation for memory efficiency