An Introduction toJVM internalsandGarbage CollectionAbhishek AsthanaSenior Member of Tech StaffOracle
Java Memory StructureHeap SpaceMethod Area /Permanent GenNative Area
Method Area StructureReservedRuntime Constant PoolField and Method DataCode-XX:PermSize-XX:MaxPermSize
Method Area• Stores per-class structures such as the run-time constant pool, field and method data,and the code for methods and constructors.• Run-time Constant Pool: contains severalkinds of constants, ranging from numericliterals known at compile-time to method andfield references that must be resolved at run-time.
Native Area• PC Register: Contains "address of theinstruction currently being executed". Ifcurrent method is native, its value isundefined.• Thread Stack: stores frames (local variables ofprimitive types, object references and partialresults) and plays a part in method invocationand return.• Native Stack: support native methods.
OutOfMemoryError – but where?• Exception in thread “main”:java.lang.OutOfMemoryError: Java heap spaceReason: an object could not be allocated into the heap space.• Exception in thread “main”:java.lang.OutOfMemoryError: PermGen spaceReason: classes and methods could not be loaded into thePermGen space. This occurs when an application requires a lotof classes e.g. in various 3rd party libraries.
• Exception in thread “main”:java.lang.OutOfMemoryError: Requestedarray size exceeds VM limitReason: this occurs when an arrays is created larger than theheap size.• Exception in thread “main”:java.lang.OutOfMemoryError: (Nativemethod)Reason: this error indicates that the problem originates froma native call rather than in the JVM.OutOfMemoryError – but where?
Garbage Collection ConceptsA garbage collector is responsible for– allocating memory– ensuring that any referenced objects remain in memory– recovering memory used by objects that are no longerreachable from references in executing code.• Objects that are referenced are said to be live• Objects that are no longer referenced areconsidered dead and are termed garbage.
Garbage Collection Concepts• GC Roots• Class and static variables - classloaded by system class loader.• Thread - live thread• Stack Local - local variable orparameter of Java method• JNI Local - local variable orparameter of JNI method• JNI Global - global JNI reference• Monitor Used - objects used as amonitor for synchronization• Held by JVM - objects held fromgarbage collection by JVM for itspurposes.GC Roots
Desirable GC Features• Safe: live data must never be erroneouslyfreed.• Comprehensive: garbage should not remainunclaimed for more than a small number ofcollection cycles.• Efficient: No long pauses during which theapplication is not running.• Limit fragmentation: Avoid memoryfragmentation.
GC Design Choices• Serial vs Parallel• Concurrent vs Stop-the-World• Compacting : moving all the live objects togetherand completely reclaiming the remaining memory.vs• Non-Compacting : releases the space utilized bygarbage objects in-placevs• Copying : copies live objects to a different memoryarea.
Key performance metrics• Throughput: % of total time not spent in garbage collection,considered over long periods of time.• Pauses: times when an application appears unresponsivebecause GC is occurring.• Frequency of collection: how often collection occurs,relative to application execution.• Promptness: time between when an object is dead andwhen memory is reclaimed.• Footprint: working set of a process, measured in pages andcache lines.
• Generations: separate pools holding objectsof different ages.• Weak Generational Hypothesis:– Most allocated objects die young.– Few references from older to younger objectsexist.Generational Collection
Young GenerationOld/TenuredReservedOld GenerationEdenS2S1-Xms-Xmx-XX:MaxNewSize-XX:NewSizeJava Heap Space StructureReservedPermGenReservedPermanent Generation
Young Generation CollectionS2EdenEmptyEmptyO1O2O3O4O5EmptyEmptyO2O3O5O1O4S1After Minor GC 1
Young Generation CollectionEmptyEmptyO2O3O5O6O7O8O9O10O5O7O10Eden S1 S2EmptyEmptyO2O3O6O8O9Eden S1 S2After Minor GC2
Old (and Perm) GenerationCollection• mark-sweep-compact collection algorithm.• Mark: the collector identifies which objectsare still live.• Sweep: “sweeps” over the generations,identifying garbage.• Compact: The collector then performs slidingcompaction, sliding the live objects towardsthe beginning of the old generation
Parallel Collector• Minor GC: uses a parallel version of the minorGC algorithm utilized by the Serial Collector.– Reduced pause times.• Full GC: same as Serial Collector– (mark-sweep-compact)• can be explicitly requested by using- XX:+UseParallelGC command line option
Parallel Compacting Collector• Minor GC: same algorithm as parallelcollector.• Full GC: same as Serial Collector– (mark-sweep-compact)• can be explicitly requested by using- XX:+UseParallelGC command line option
CMS Collector• Concurrent Mark and Sweep1. Initial marking: GC root objects marked alive.Complete ‘Stop-the-World’.2. Concurrent marking: marked root objects aretraversed and all reachable objects aremarked. Another round of marking of objectsallocated during this phase!3. Final marking: Stop-the-World and all remainingnewly allocated objects are marked alive.4. Sweep!
Collection of GarbageGarbage Collector!
Garbage First (G1) Collector• The heap is partitioned into a set of equal-sizedheap regions, each a contiguous range of virtualmemory• Concurrent marking phase to determine livenessof objects across the heap.• After the mark phase completes, G1 focuses onthe regions that are likely to be full of garbage.• Copies objects to a single region on the heap, andin the process both compacts and frees upmemory