Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 24

Object Oriented Paradigm

Garbage Collection
Major Areas of Memory

• Static area
− Fixed size, fixed content, allocated at compile time

• Run-time stack
− Variable size, variable content (activation records)
− Used for managing function calls and returns

• Heap
− Fixed size, variable content
− Dynamically allocated objects and data structures
− Examples: ML reference cells, malloc in C, new in Java

2
Object De-allocation
• How are objects deallocated?
• Storage classes:
static - never deallocated, same lifetime as the program
stack - deallocated automatically on subroutine return
heap - explicit or implicit deallocation

• Explicit deallocation in Pascal:


dispose (my_ptr);
− In C:
free (my_ptr);
− In C++:
delete my_ptr;

• Implicit deallocation
− Garbage collection
3
Garbage

• Garbage is a block of heap memory that cannot be


accessed by the program
− An allocated block of heap memory does not have a reference to it
(memory leak)

− Another kind of memory error: a reference exists to a block of


memory that is no longer allocated (Dangling reference)

• Garbage collection (GC) - automatic management of


dynamically allocated storage
− Reclaim unused heap blocks for later use by program

4
Dangling References

• Dangling reference - a pointer that no longer points to a live


object
• Produced in the context of heap allocation:

5
Example of Garbage

class node {
int value; p = new node();
node next; q = new node();
} q = p;
node p, q; delete p;

6
Dangling References
• Dangling references can only be produced in languages
with explicit deallocation.
− Programmer may deallocate an object that is still referred by live
pointers
− Garbage collection will check if an object still has references to it
before deleting it

• Why do we need to detect dangling references?


− Need to warn the programmer - generate an error

• Mechanisms to detect dangling references


− Tombstones
− Locks and keys

7
Tombstones

• Tombstone itself is a pointer which points to the


actual heap dynamic variable
• The actual pointer variable never points to head
dynamic variable

8
Tombstones
• Tombstones are special heap cells that represent
proxies for allocated heap cells.

9
Tombstones

10
Lock-and-key

11
Lock-and-key

12
Lock-and-key

13
Garbage Collection
• Garbage collection
− Essential for functional languages - frequent construction and return
of objects from functions

− Increasingly used in imperative languages (Ada, Java)

− Mechanisms:
− Reference Counter
− Mark-and-Sweep

14
Reference Counter
• How do we know when a heap object is no longer useful?
− when there are no pointers to it
• Solution
− in each object keep a reference counter = the number of pointers
referring the object
− when allocating the object:
int r; Student p;
st = new Student();

− when assigning pointers:


p = st; // refcnt of object referred by r ++

− When a pointer is disconnected then do the decrement operation


delete p; // refcnt of object referred by r --

− when the reference count is zero → can destroy it


15
Reference Counting: Example

Heap space

root
set 1

1 1 1

1 2 1

16
Reference Counts
• Problem - circular lists:

• The objects in the list are not reachable, but their reference
counts are non-zero
17
Reference Counter

− Directly keeps track of live cells

− Doesn’t detect all garbage

18
Mark-and-Sweep Collection

• When the free space becomes low:

1. walk through the heap, and mark each block


(tentatively) as "useless“

2. recursively explore all pointers in the program, and


mark each encountered block as "useful“

3. walk again through the heap, and delete every


"useless" block (could not be reached)

19
Mark-Sweep Example

Heap space

root
set

slide 20
Mark-Sweep Example

Heap space

root
set

slide 21
Mark-Sweep Example

Heap space

root
set

slide 22
Mark-Sweep Example

Heap space
Free unmarked
root cells
set

Reset mark bit


of marked cells

slide 23
GC and Programming Languages

• GC is not a language feature


• GC is a pragmatic concern for automatic and
efficient heap management
− Cooperative langs: Lisp, Scheme, Prolog, Smalltalk …
− Uncooperative languages: C and C++
− But garbage collection libraries have been built for C/C++

• Recent GC revival
− Object-oriented languages: Modula-3, Java
− In Java, runs as a low-priority thread; System.gc may be called
by the program
− Functional languages: ML and Haskell

24

You might also like