Garbage Collection in Hotspot JVM
- 2. Explicit Memory Management malloc/new allocates space for an object free/delete returns memory to system Forget to free -> memory leak
- 4. Garbage Collection "Heap storage for objects is reclaimed by an automatic storage management system (typically a garbage collector); objects are never explicitly deallocated."
- 5. — Java Virtual Machine Specification, Section 3.5.3 [JVMS2 1999]
- 6. An object that is still referenced somewhere will never be garbage collected
- 11. Solves many but not all, create objects indefinitely and continue referencing them.
- 20. Pause time —the length of time during which application execution is stopped while garbage collection is occurring.
- 23. Promptness —the time between when an object becomes garbage and when the memory becomes available.
- 24. Reachability Roots – reference to object in a static variable or local variable on an active stack frame
- 29. 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
- 36. Reference Counting Add a reference count field for every object and manipulate whenever number of references change.
- 40. Mark - Sweep Root 1. mark-sweep start Root 2. end of marking Root 3. end of sweeping
- 42. Two strategies include: Mark-Compact Collector – After mark, moves live objects to a contiguous area in the heap
- 44. Mark - Compact proportional to the size of the heap Root 1. end of marking Root 2. end of compacting
- 45. Copy Collector proportional to the number of live objects Root 1. copying start Root 2. end of copying Unused Unused From From To To
- 46. Weak generational hypothesis Two very interesting Observations Most allocated objects are not referenced (considered live) for long, that is, they die young.
- 50. Majority of GC activity takes place here and is usually fast (Minor GC).
- 51. Time intensive Old generation long lived objects are promoted (or tenured) to the old generation.
- 55. Before Minor GC Old Permanent Eden From To Survivor Spaces Unused Young
- 56. After Minor GC Empty Old Permanent Eden To From Survivor Spaces Unused Young
- 60. All are Generational. Collector GC Used Serial “ Serial" + "Serial Old" Parallel (Throughput) "Parallel" + "Serial Old" Parallel Compaction "Parallel" + "Parallel Old" Concurrent Mark-Sweep (Low Pause) "ParNew" + "CMS”
- 67. Old : Mark-sweep-compact. Sliding compaction : sliding the live objects towards the beginning of the old generation space. -XX:+UseSerialGC
- 73. Old : Mark-sweep-compact (same as Serial).
- 77. Old Generation – Parallel Compaction each generation is logically divided into fixed-sized regions.
- 83. Marks all live objects that are transitively reachable from this set concurrently.
- 89. may split or join free blocks to meet demand. Initial Mark Phase Concurrent Mark Phase Remark phase Concurrent sweep phase
- 90. CMS Collector stop-the-world pause Mark-Sweep-Compact Collector Concurrent Mark-Sweep Collector initial mark concurrent marking remark concurrent sweeping
- 94. Size areas of heap based on application profile – e.g., an app that creates many temporary objects may need a large eden area
- 100. A large heap may slow an app (more memory is not always good)