Your SlideShare is downloading. ×
0
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

An Introduction to JVM Internals and Garbage Collection in Java

5,936

Published on

Java heap structure, Generations, different Garbage Collection approaches and

Java heap structure, Generations, different Garbage Collection approaches and

Published in: Technology
2 Comments
30 Likes
Statistics
Notes
No Downloads
Views
Total Views
5,936
On Slideshare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
0
Comments
2
Likes
30
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. An Introduction toJVM internalsandGarbage CollectionAbhishek AsthanaSenior Member of Tech StaffOracle
  • 2. Java Memory StructureHeap SpaceMethod Area /Permanent GenNative Area
  • 3. Method Area StructureReservedRuntime Constant PoolField and Method DataCode-XX:PermSize-XX:MaxPermSize
  • 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. Native Area StructureThread 1 … NProgram Counter(PC) RegisterThread Stack Native Stack
  • 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. Example (what goes where!)
  • 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. 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. • 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. 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. 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. 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. 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. 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. • 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. Young GenerationOld/TenuredReservedOld GenerationEdenS2S1-Xms-Xmx-XX:MaxNewSize-XX:NewSizeJava Heap Space StructureReservedPermGenReservedPermanent Generation
  • 18. Young Generation CollectionS2EdenEmptyEmptyO1O2O3O4O5EmptyEmptyO2O3O5O1O4S1After Minor GC 1
  • 19. Young Generation CollectionEmptyEmptyO2O3O5O6O7O8O9O10O5O7O10Eden S1 S2EmptyEmptyO2O3O6O8O9Eden S1 S2After Minor GC2
  • 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. Comparing GC StrategiesPausePauseConcurrent MarkRe-MarkSerial Parallel ConcurrentApplication Thread GC Thread
  • 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. 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. 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. Collection of GarbageGarbage Collector!
  • 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. JConsole
  • 28. Thank You!

×