An Introduction to JVM Internals and Garbage Collection in Java


Published on

Java heap structure, Generations, different Garbage Collection approaches and

Published in: Technology
  • Good Article but i need to download this file
    Are you sure you want to  Yes  No
    Your message goes here
  • If you're interested in realtime garbage collection monitoring you can use this free VisualVM plugin:
    You can see many of presented concept on your own systems.
    Are you sure you want to  Yes  No
    Your message goes here
  • Good one
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

An Introduction to JVM Internals and Garbage Collection in Java

  1. 1. An Introduction toJVM internalsandGarbage CollectionAbhishek AsthanaSenior Member of Tech StaffOracle
  2. 2. Java Memory StructureHeap SpaceMethod Area /Permanent GenNative Area
  3. 3. Method Area StructureReservedRuntime Constant PoolField and Method DataCode-XX:PermSize-XX:MaxPermSize
  4. 4. 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.
  5. 5. Native Area StructureThread 1 … NProgram Counter(PC) RegisterThread Stack Native Stack
  6. 6. 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.
  7. 7. Example (what goes where!)
  8. 8. Heap Method Area Thread StackObject: FileInputStream Class: FileTest Reference: streamObject: Scanner Class: FileInputStream Reference: scannerObject: String Class: Scanner Reference: lineObject: Exception Class: Exception Reference: eExample (what goes where!)
  9. 9. 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.
  10. 10. • 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?
  11. 11. 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.
  12. 12. 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
  13. 13. 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.
  14. 14. 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.
  15. 15. 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.
  16. 16. • Generations: separate pools holding objectsof different ages.• Weak Generational Hypothesis:– Most allocated objects die young.– Few references from older to younger objectsexist.Generational Collection
  17. 17. Young GenerationOld/TenuredReservedOld GenerationEdenS2S1-Xms-Xmx-XX:MaxNewSize-XX:NewSizeJava Heap Space StructureReservedPermGenReservedPermanent Generation
  18. 18. Young Generation CollectionS2EdenEmptyEmptyO1O2O3O4O5EmptyEmptyO2O3O5O1O4S1After Minor GC 1
  19. 19. Young Generation CollectionEmptyEmptyO2O3O5O6O7O8O9O10O5O7O10Eden S1 S2EmptyEmptyO2O3O6O8O9Eden S1 S2After Minor GC2
  20. 20. 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
  21. 21. Comparing GC StrategiesPausePauseConcurrent MarkRe-MarkSerial Parallel ConcurrentApplication Thread GC Thread
  22. 22. 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
  23. 23. 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
  24. 24. 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!
  25. 25. Collection of GarbageGarbage Collector!
  26. 26. 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
  27. 27. JConsole
  28. 28. Thank You!