Docsity

Memory Management and Object Deallocation in OOP: Garbage Collection, Study notes of Object Oriented Programming

Partial preview of the text

Download Memory Management and Object Deallocation in OOP: Garbage Collection 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

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

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

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

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

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

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 st; // refcnt of new object โ† 1 p = new int; โˆ’ (^) 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

Reference Counts

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

Reference Counter โˆ’ (^) Directly keeps track of live cells โˆ’ (^) Doesnโ€™t detect all garbage

slide 16 root set Heap space

Mark-Sweep Example

slide 17 root set Heap space

Mark-Sweep Example

slide 19 root set Heap space

Mark-Sweep Example

Reset mark bit of marked cells Free unmarked cells

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