Docsity

Garbage Collection: Techniques and Algorithms, Study notes of Computer Science

Toggle sidebar
bg1
1
Prof. Su ECS 142 Lecture 18 1
Automatic Memory Management
ECS 142
Prof. Su ECS 142 Lecture 18 2
Lecture Outline
• Why Automatic Memory Management?
• Garbage Collection
• Three Techniques
– Mark and Sweep
– Stop and Copy
– Reference Counting
Prof. Su ECS 142 Lecture 18 3
Why Automatic Memory Management?
• Storage management is still a hard problem in
modern programming
• C and C++ programs have many storage bugs
– forgetting to free unused memory
– dereferencing a dangling pointer
– overwriting parts of a data structure by accident
– and so on...
• Storage bugs are hard to find
– a bug can lead to a visible effect far away in time
and program text from the source
Prof. Su ECS 142 Lecture 18 4
Type Safety and Memory Management
• Some storage bugs can be prevented in a
strongly typed language
– e.g., you cannot overrun the array limits
• Can types prevent errors in programs with
manual allocation and deallocation of memory?
– Some fancy type systems (linear types) were
designed for this purpose but they complicate
programming significantly
• If you want type safety then you must use
automatic memory management
Prof. Su ECS 142 Lecture 18 5
Automatic Memory Management
• This is an old problem:
– Studied since the 1950s for LISP
• There are several well-known techniques for
performing completely automatic memory
management
• Until recently they were unpopular outside the
Lisp family of languages
– just like type safety used to be unpopular
Prof. Su ECS 142 Lecture 18 6
The Basic Idea
• When an object that takes memory space is
created, unused space is automatically
allocated
– In Cool, new objects are created by new X
• After a while there is no more unused space
• Some space is occupied by objects that will
never be used again
• This space can be freed to be reused later
pf3
pf4
pf5
pf8

Partial preview of the text

Download Garbage Collection: Techniques and Algorithms and more Study notes Computer Science in PDF only on Docsity!

Prof. Su ECS 142 Lecture 18 1

Automatic Memory Management

ECS 142

Prof. Su ECS 142 Lecture 18 2

Lecture Outline

  • Why Automatic Memory Management?
  • Garbage Collection
  • Three Techniques
    • Mark and Sweep
    • Stop and Copy
    • Reference Counting

Prof. Su ECS 142 Lecture 18 3

Why Automatic Memory Management?

  • Storage management is still a hard problem in

modern programming

  • C and C++ programs have many storage bugs
    • forgetting to free unused memory
    • dereferencing a dangling pointer
    • overwriting parts of a data structure by accident
    • and so on...
  • Storage bugs are hard to find
    • a bug can lead to a visible effect far away in time and program text from the source Prof. Su ECS 142 Lecture 18 4

Type Safety and Memory Management

  • Some storage bugs can be prevented in a

strongly typed language

  • e.g., you cannot overrun the array limits
  • Can types prevent errors in programs with

manual allocation and deallocation of memory?

  • Some fancy type systems (linear types) were designed for this purpose but they complicate programming significantly
  • If you want type safety then you must use

automatic memory management

Prof. Su ECS 142 Lecture 18 5

Automatic Memory Management

  • This is an old problem:
    • Studied since the 1950s for LISP
  • There are several well-known techniques for

performing completely automatic memory

management

  • Until recently they were unpopular outside the

Lisp family of languages

  • just like type safety used to be unpopular

Prof. Su ECS 142 Lecture 18 6

The Basic Idea

  • When an object that takes memory space is

created, unused space is automatically

allocated

  • In Cool, new objects are created by new X
  • After a while there is no more unused space
  • Some space is occupied by objects that will

never be used again

  • This space can be freed to be reused later

Prof. Su ECS 142 Lecture 18 7

The Basic Idea (Cont.)

  • How can we tell whether an object will “never

be used again”?

  • In general it is impossible to tell
  • We will have to use a heuristic to find many (not all) objects that will never be used again
  • Observation: a program can use only the

objects that it can find:

let x : A  new A in { x  y; ... }

  • After x  y there is no way to access the newly allocated object Prof. Su ECS 142 Lecture 18 8

Garbage

  • An object x is reachable if and only if:
    • A register contains a pointer to x, or
    • Another reachable object y contains a pointer to x
  • You can find all reachable objects by starting

from registers and following all the pointers

  • An unreachable object can never by referred

by the program

  • These objects are called garbage

Prof. Su ECS 142 Lecture 18 9

Reachability is an Approximation

  • Consider the program: x  new A; y  new B x  y; if alwaysTrue() then x  new A else x.foo() fi
  • After x  y (assuming y becomes dead there)
    • The object A is not reachable anymore
    • The object B is reachable (through x)
    • Thus B is not garbage and is not collected
    • But object B is never going to be used

Prof. Su ECS 142 Lecture 18 10

Tracing Reachable Values in Coolc

  • In coolc, the only register is the accumulator
    • it points to an object
    • and this object may point to other objects, etc.
  • The stack is more complex
    • each stack frame contains pointers
      • e.g., method parameters
    • each stack frame also contains non-pointers
      • e.g., return address
    • if we know the layout of the frame we can find the pointers in it

Prof. Su ECS 142 Lecture 18 11

A Simple Example

  • In Coolc we start tracing from acc and stack
    • they are called the roots
  • Note that B and D are not reachable from acc

or the stack

  • Thus we can reuse their storage

A B C

Frame 1 Frame 2

acc D E

SP

Prof. Su ECS 142 Lecture 18 12

Elements of Garbage Collection

  • Every garbage collection scheme has the

following steps

  1. Allocate space as needed for new objects
  2. When space runs out: a) Compute what objects might be used again (generally by tracing objects reachable from a set of “root” registers) b) Free the space used by objects not found in (a)
  • Some strategies perform garbage collection

before the space actually runs out

Prof. Su ECS 142 Lecture 18 19

Mark and Sweep: Details

  • The todo list is used as an auxiliary data

structure to perform the reachability analysis

  • There is a trick that allows the auxiliary data

to be stored in the objects themselves

  • pointer reversal: when a pointer is followed it is reversed to point to its parent
  • Similarly, the free list is stored in the free

objects themselves

Prof. Su ECS 142 Lecture 18 20

Mark and Sweep. Evaluation

  • Space for a new object is allocated from the

new list

  • a block large enough is picked
  • an area of the necessary size is allocated from it
  • the left-over is put back in the free list
  • Mark and sweep can fragment the memory
  • Advantage: objects are not moved during GC
  • no need to update the pointers to objects
  • works for languages like C and C++

Prof. Su ECS 142 Lecture 18 21

Another Technique: Stop and Copy

  • Memory is organized into two areas
    • Old space: used for allocation
    • New space: used as a reserve for GC

old space new space

heap pointer

  • The heap pointer points to the next free word

in the old space

  • Allocation just advances the heap pointer Prof. Su ECS 142 Lecture 18 22

Stop and Copy Garbage Collection

  • Starts when the old space is full
  • Copies all reachable objects from old space

into new space

  • garbage is left behind
  • after the copy phase the new space uses less space than the old one before the collection
  • After the copy the roles of the old and new

spaces are reversed and the program resumes

Prof. Su ECS 142 Lecture 18 23

Stop and Copy Garbage Collection. Example

root A B C D E F

Before collection:

new space

A C F

root

new space

After collection:

free

heap pointer

Prof. Su ECS 142 Lecture 18 24

Implementation of Stop and Copy

  • We need to find all the reachable objects, as

for mark and sweep

  • As we find a reachable object we copy it into

the new space

  • And we have to fix ALL pointers pointing to it!
  • As we copy an object we store in the old copy

a forwarding pointer to the new copy

  • when we later reach an object with a forwarding pointer we know it was already copied

Prof. Su ECS 142 Lecture 18 25

Implementation of Stop and Copy (Cont.)

  • We still have the issue of how to implement

the traversal without using extra space

  • The following trick solves the problem:
    • partition the new space in three contiguous regions

copied and scanned

scan

copied objects whose pointer fields were followed and fixed

copied objects whose pointer fields were NOT followed

copied empty

start alloc

Prof. Su ECS 142 Lecture 18 26

Stop and Copy. Example (1)

root A B C D E F new space

  • Before garbage collection

start scan alloc

Prof. Su ECS 142 Lecture 18 27

Stop and Copy. Example (3)

root A B C D E F

  • Step 1: Copy the objects pointed by roots and

set forwarding pointers (dotted arrow)

A

start scan

alloc

Prof. Su ECS 142 Lecture 18 28

Stop and Copy. Example (3)

root A B C D E F

  • Step 2: Follow the pointer in the next

unscanned object (A)

  • copy the pointed objects (just C in this case)
  • fix the pointer in A
  • set forwarding pointer

A

scan

alloc

C

start

Prof. Su ECS 142 Lecture 18 29

Stop and Copy. Example (4)

root A^ B C D E F

  • Follow the pointer in the next unscanned

object (C)

  • copy the pointed objects (F in this case)

A

scan

alloc

C F

start

Prof. Su ECS 142 Lecture 18 30

Stop and Copy. Example (5)

root A^ B C D E F

  • Follow the pointer in the next unscanned

object (F)

  • the pointed object (A) was already copied. Set the pointer same as the forwading pointer

A

scan

alloc

C F

start

Prof. Su ECS 142 Lecture 18 37

Reference Counting [Collins 1960]

  • Rather that wait for memory to be exhausted,

try to collect an object when there are no

more pointers to it

  • Store in each object the number of pointers

to that object

  • This is the reference count
  • Each assignment operation has to manipulate

the reference count

Prof. Su ECS 142 Lecture 18 38

Implementation of Reference Counting

  • new returns an object with a reference count

of 1

  • If x points to an object then let rc(x) refer to

the object’s reference count

  • Every assignment x  y must be changed: rc(y)  rc(y) + 1 rc(x)  rc(x) - 1 if(rc(x) == 0) then mark x as free x  y

Prof. Su ECS 142 Lecture 18 39

Reference Counting. Evaluation

  • Advantages:
    • Easy to implement
    • Collects garbage incrementally without large pauses in the execution
  • Disadvantages:
    • Manipulating reference counts at each assignment is very slow
    • Cannot collect circular structures

Prof. Su ECS 142 Lecture 18 40

Garbage Collection. Evaluation

  • Automatic memory management avoids some

serious storage bugs

  • But it takes away control from the

programmer

  • e.g., layout of data in memory
  • e.g., when is memory deallocated
  • Most garbage collection implementation stop

the execution during collection

  • not acceptable in real-time applications

Prof. Su ECS 142 Lecture 18 41

Garbage Collection. Evaluation

  • Garbage collection is going to be around for a

while

  • Researchers are working on advanced garbage

collection algorithms:

  • Concurrent: allow the program to run while the collection is happening
  • Generational: do not scan long-lived objects at every collection (JVM uses this kind)
  • Parallel: several collectors working in parallel

Prof. Su ECS 142 Lecture 18 42

Topics Not Covered

  • Lambda Calculus
    • Foundation for Programming Languages
  • Instruction Scheduling
  • Compiling Exceptions

Prof. Su ECS 142 Lecture 18 43

Beyond Compilers (Applications)

  • Type Systems
    • Enforce Memory Safety
    • Detect Programming Errors
      • Format-string, SQL injection, User-kernel pointer …
  • Data-Flow Analysis
    • Enable More Advanced Compiler Optimizations
    • Detect Programming Errors
      • Buffer overflows, Race conditions, File handling …
      • Worms, viruses …
  • A Big Area
    • Software Reliability and Security
    • Compiler- or PL-based techniques