Docsity

OOPS - GarbageCollection 1, Study notes of Object Oriented Programming

Partial preview of the text

Download OOPS - GarbageCollection 1 and more Study notes Object Oriented Programming in PDF only on Docsity!

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

Heap Storage Deallocation Examples:

  • (^) Implicit: Java, Scheme
  • (^) Explicit: Pascal and C
  • (^) Implicit and Explicit: Ada
  • (^) Explicit deallocation To free heap memory a specific operation must be called. − (^) in Pascal: dispose (my_ptr); − (^) In C: free (my_ptr); − (^) In C++: delete my_ptr;

Cells and Liveness

  • (^) Cell = data item in the heap − (^) Cells are “pointed to” by pointers held in registers, stack, global/static memory, or in other heap cells
  • (^) Roots: registers, stack locations, global/static variables
  • (^) A cell is live if its address is held in a root or held by another live cell in the heap

Dangling References

  • (^) Dangling reference - a pointer that no longer points to a live object
  • (^) Produced in the context of heap allocation: p = new int ; r = p ; delete r ; *p = 3; // crash!!

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

Garbage Collection − (^) Advantages: implementation simplicity, speed − (^) Disadvantages: burden on programmer, may produce garbage (memory leaks or dangling references)

  • (^) Explicit deallocation of heap objects
  • (^) Automatic deallocation of heap objects (garbage collection) − (^) Advantages: convenience for programmer, safety (no memory leaks or dangling references) − (^) Disadvantages: complex implementation, run-time overhead

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

01 0 0 0 Reference Counting Example 1 2 1 1 root set Heap space

2 1 1 1 Reference Counting Example 0 1 root set Heap space

2 1 1 1 Reference Counting Example 0 0 0 1 1 Returned to free list root set Heap space

Reference Counts

  • (^) Problem - circular lists:
  • (^) The objects in the list are not reachable, but their reference counts are non-zero

Mark-and-Sweep Collection

  • (^) John McCarthy, 1960 (first LISP implementation) Mark and Sweep method works in two phases
  1. Mark the records reachable from the roots
  2. Sweep the heap space by moving unreachable records to the freelist When the free space becomes low:
    1. Walk through the heap, and mark each block as "useless“
    2. Starting at each symbol perform a depth-first search marking all blocks reachable ie., recursively explore all pointers in the program, and mark each encountered block as "useful“
    3. Sweep through all blocks. − (^) If marked: Unmark − (^) If unmarked: move to free list

slide 20 root set Heap space

Mark-Sweep Example