1. Garbage Collection in Java“Heap storage for objects is reclaimed by an automatic storage management system. Objects are never explicitly de-allocated” –Java Virtual Machine Specification 
2. Java Variable Flavours• Stack – Local variables holding primitives. 10 – Local variable of reference typewill point at heap memory.• Heap – Objects. – Primitive fields of an object.
3. Mark and Sweep Approach• Simplest. Garbage collector algorithm.• Recover and reuse.Heap memoryno longer in use.• Algorithm. – Stop-the-world.Non-deterministicpauses stop allthe running threads. – Start.From live objectswalks through the tree of references. – Mark.As liveany object on the route. – Sweep.Everything left is garbage and can be collected.
4. Generational Garbage Collector• Mark-and-sweep. Improved.• Weak generational hypothesis . – Mostobjectsbecome unreachable quickly. – Fewreferencesfrom older to young Eden Young Generation objects exist.• Areas of memory. Survivor – Eden.Most new objects (very large From To objects directly to old generation). Generation – Survivor.Survived, one holds Old Tenured object, the other empty. – Tenured.Promoted longer-lived objects. PermGen – PermGen.Not strictly in heap, internal structures (i.e. class definitions).
5. Escape Analysis• Recent change.Java 6u23.• Local variables.Onlyused inside the method. – No passedinto other methods. – No returned.• No heap.Object created on the method stack frame – Reduce objects of young 10 collections. – Memory used freed when method returns.
6. Concurrent Mark-Sweep in Action• Two short pauses.Per GC cycle, initial mark and remark. Marking/Pre-cleaning Sweeping• Initial Mark.Identifies set of objectsimmediately reachable outside old generation.• Concurrent marking phase.Marks all live objectstransitively reachable from this set. • Object graph can change.Not alllive objects are guaranteed to be marked.• Pre-cleaning.Revisitingobjects modified concurrently with the marking phase. Initial Mark Remark• Second Pause (Remark).Revisits objects modified during concurrent marking phase. Running application thread• Concurrent sweep phase.Deallocates garbage Running GC thread objects without relocatingthe live ones.
7. CMS Pros/Cons• Advantages. – Two pauses. No one single pause. – Concurrency. App and GC run in parallel.• Disadvantages. Start sweeping – Extra overhead. – Freelists. Free space not contiguous. – Large Java heap req.Marking cycle lasts morethan stop-the-world, spacereclaimed at End sweeping the end. – App runs concurrently.Old generationpotentially increases during marking phase. – Floating garbage. Not guaranteed all garbage objects. – Fragmentation issue.Lack of compaction, possible not efficient use of free space .
8. Garbage-First GC (G1) in Action• Features. Parallel, concurrent, incrementally compacting low-pause.• Heap layout.Split into regions.• Region.Equal-sized chunks.• Pause goal.How long app can pause for GC while running (20 ms every 5 min).• Pause.Objects evacuatedfrom one or Marking more regions to a single region.• Statistics.Average a region takes to collect.• G1?. Knows mostly empty regions. – Collects.Inthis regions first. – Concentrates.Collect on areas likely GC GC GC to be full of garbage. Running application thread Running GC thread
9. G1 Pros/Cons• Advantages. – High performance. – Pause time goals. Prevent interruption proportional to heap or live-data size. – Compact and free up memory.Continuously work to reduce fragmentation. – Concurrent global marking phase. Determine liveness of the objects. – Pause prediction model. • Meet user-defined pause time target with high probability. • Selects number of regions to collect based on the time target.• Disadvantages. – Target. Multi-processor machines with large memories.
10. Comparison• G1. • CMS. – Compacting collector. – No compaction. – Avoid free list, rely on regions. – No control. – More predictable pauses. • ParallelOld. – User can specify pause targets. – Whole-heap compaction, long• Switch to G1. pauses. – More than 50% heap occupied – No control. with live data. – Allocation and promotion rate varies significantly. – Undesired long collection and compaction pauses (0.5s to 1s)
11. References Lindholm, Tim, and Frank Yellin. Java Virtual Machine Specification, Second Edition. Addison-Wesley, Reading, MA, 1999. Charlie Hunt, Binu John. Java Performance, First Edition. Pearson, 2011. Jones, Richard, and Rafael Lins. Garbage Collection. John Wiley & Sons, Ltd.,WestSussex, PO19 IUD, England, 1996. Memory Management in the Java HotSpot Virtual Machine. Sun Mircrosystem, April 2006. B.J. Evans, M. Verburg. Well-Grounded Java Developer. Manning, 2012.