Docsity

Understanding Garbage Collection: A Comprehensive Lecture - Prof. Elsa Gunter, Study notes of Computer Science

Partial preview of the text

Download Understanding Garbage Collection: A Comprehensive Lecture - Prof. Elsa Gunter and more Study notes Computer Science in PDF only on Docsity!

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

CS421 Lecture 20: Garbage Collection

Mark Hills

[email protected]

University of Illinois at Urbana-Champaign

July 24, 2008

1 Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul

Agha, and Elsa Gunter

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

(^1) Motivation

(^2) Terminology

(^3) Reference Counting

(^4) Mark/Sweep

(^5) Copy Collectors (Scavengers)

(^6) Extensions

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Memory Allocation Why GC?

Stack Allocation

Memory is allocated when procedures are called; lifetime is

the lifetime of the stack frame

Flexible: can now support recursion, local data structures with

size dependent on parameter values

Slightly dynamic: stack frames can outlive caller, provide

limited version of “higher-order” functions

Limited: procedure results limited to memory objects of a size

known at compile time (since we need to know how much

memory to use for it)

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Memory Allocation Why GC?

Heap Allocation

Memory is allocated dynamically; lifetime dependent on usage

of memory

Flexible: allows for creation of memory objects with dynamic

sizes, recursive structures (lists, trees, etc)

Can return results of non-fixed size from procedures (in

general, result has “fixed” size as size of reference or pointer)

More natural model for higher-order functions (closures) – can

live for arbitrary amount of time

Downside: allocation and disposal more involved, requires

explicit memory management or garbage collection

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Memory Allocation Why GC?

Garbage Collection

What is garbage collection?

Garbage collection is “the automatic management of dynamically

allocated storage” (from Jones, Garbage Collection)

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Memory Allocation Why GC?

Why not Explicit Memory Management

Advantage: high performance

Disadvantage: double frees, memory leaks, dangling

references, plus very challenging with some languages

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Memory Allocation Why GC?

Memory Leaks

Memory object should be accessible as long as something

points to it

Memory not freed before last pointer to it is removed “leaks”

Common source of out of memory errors

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Memory Allocation Why GC?

Dangling References

Aliasing means that multiple variables could hold a reference

to the same memory object

Free’ing one variable, without cleaning up others, means that

reference to freed memory still exists

These references are “dangling references”; attempts to

access memory through these can be disastrous

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Memory Allocation Why GC?

Purpose of Garbage Collection

Garbage collection solves these problems by removing

responsibility of deleting allocated storage from user

User generally still explicitly allocates new storage

Language runtime recovers used storage when certain

conditions are met, based on features of collector

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Roots

Roots provide a limited set of pointers into dynamically

allocated memory

Idea: all reachable memory should be reachable starting at

one of the roots

Example roots: global variables, object fields, function

parameters on the stack

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Mutator and Collector

Garbage collector sees layout of memory as primary data

structure

Running program changes (i.e. mutates) this memory layout –

running program referred to as the mutator

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Reference Counting: The Basics

Memory objects extended with a counter

Memory operations increment/decrement counter, based on

type of operation (e.g., assigning a pointer to an object

increments the counter of the object assigned to)

Objects are garbage when counter goes to 0; can be

immediately collected

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Disadvantages

Pay a “tax” on each memory operation

Reference counter has to know details about language’s

memory operations, tied very closely to language semantics

Incrementing counter takes CPU cycles

Cyclic structures pose challenges

Motivation Terminology Reference Counting Mark/Sweep Copy Collectors (Scavengers) Extensions

Mark/Sweep Collection: The Basics

Collection starts with a predefined set of roots (mentioned

earlier)

Live objects reached by “tracing” pointer paths, setting a

mark bit on memory objects

Once all objects traced, unmarked objects discarded (sweep)