Garbage Collection

Garbage collection (GC) is a form of automatic memory management.

Manual Memory Management

Manually free up memory after completion of usage, Here there is a chance of memory leak

Problems with this approach

  • Dangling references: It is possible to deallocate the space used by an object to which some other object still has a reference.
  • Space Leaks: This occurs when memory is allocated and no longer referenced but is not released.

Automated Memory Management

Automated Memory Management(Garbage Collection) solves both the problems mentioned in Manual memory management

Initial approach was reference counting

Live Object - Reachable (Referenced by someone else) Dead Object - UnReachable (Unreferenced from any where)

Properties of Garbage Collector

Garbage collector should operate efficiently without introducing long pauses during which the application is not running

  • If heap size is small, Collection will be fast, but heap will get filled fast
  • If heap size is big, heap will get filled slow, but collection will take longer time

Another important characterstic is Fragmentation

Steps

Mark Starts from root node of application, walks the object graph, mark objects that are reachable as live

Sweep (Delete) Delete unreachable objects

Compact Compacting the memory moving around the objects and making allocation contiguous

Here cycles won't be leaked as above

One drawback in mark and sweep is application need to be paused while mark phase is going on, which will cause latency issues During sweep phase application can run in parallel

results matching ""

    No results matching ""