SlideShare a Scribd company logo
Memory Management In Hotspot VM (GarbageCollection) Jaganmohan Reddy
Explicit Memory Management malloc/new allocates space for an object free/delete returns memory to system Forget to free -> memory leak
Free too soon ->  “dangling pointer”
Garbage Collection "Heap storage for objects is reclaimed by an automatic storage management system (typically a garbage collector); objects are never explicitly deallocated."
—  Java Virtual Machine Specification, Section 3.5.3 [JVMS2 1999]
An object that is still referenced somewhere will never be garbage collected
Automatically frees all memory no longer referenced
Garbage Collector Allocating memory
Ensuring that any referenced objects remain in memory
Recovering memory used by objects that are no longer reachable from references in executing code
Solves many but not all, create objects indefinitely and continue referencing them.
Garbage Collection Algorithm Garbage Object Detection Live Object (referenced)
Dead Object (not referenced) Garbage Objects Heap memory reclaim
Design Choices Serial Vs Parallel.
Concurrent Vs Stop-the-world.
Compacting Vs Non Compacting Vs Copying.
Interactive Vs Non interactive apps.
Performance Metrics Throughput —the percentage of total time not spent in garbage collection, considered over long  periods of time.
Garbage collection overhead —the inverse of throughput, that is, the percentage of total time spent in  garbage collection.
Pause time —the length of time during which application execution is stopped while garbage  collection is occurring.
Frequency of collection —how often collection occurs, relative to application execution.
Footprint —a measure of size, such as heap size.
Promptness —the time between when an object becomes garbage and when the memory becomes  available.
Reachability Roots – reference to object in a static variable or local variable on an active stack frame
Root Objects – directly reachable from roots
Live Objects – all objects transitively reachable from roots
Garbage Objects – all other objects
Reachability Example Runtime Stack ref ref Heap   
GC Approaches Two basic approaches: Reference Counting – keep a count of references to an object; when a count of zero, object is garbage  Tracing – trace out the graph of objects starting from roots and mark; when complete, unmarked objects are garbage mark phase –  GC traverses reference tree, marking objects
sweep phase  – unmarked objects are finalized/freed
Garbage Collection Algorithms Reference Counting algorithm
Mark-and-sweep algorithm
Copying algorithm
Mark-and-Compacting algorithm

More Related Content

Garbage Collection in Hotspot JVM