Quick introduction to Java Garbage Collector (JVM GC)


Published on

Published in: Technology
1 Comment
1 Like
  • If you're interested in realtime garbage collection monitoring you can also take a look at this free VisualVM plugin: http://www.spyglasstools.com/documentation/spyglass-garbage-collector-analyzer/
    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

Quick introduction to Java Garbage Collector (JVM GC)

  1. 1. JVM and Garbage Collection by marcos.garcia@savoirfairelinux.com, Jan 2013
  2. 2. JVM?● Bytecode interpreter http://viralpatel.net/blogs/java-virtual-machine-an-inside-story/
  3. 3. JVM Memory Vocabulary● Runtime data areas – PC register: ● One for each thread ● Points to current execution code of each thread – JVM stacks ● One dedicated stack for each thread ● Holds many stack frames, one for each invoked method – Every frame has 3 registers – Heap ● Shared among all threads ● Stores Java objects. ● Cleaned up by GC – Method area ● Shared among all threads ● Contains method code (class implementations) ● Has a Runtime constant pool – Native method stack (just for “native methods” http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5
  4. 4. JVM Stack● Stack Frame – Used to store data and partial results, as well as to perform dynamic linking, return values for methods, and dispatch exceptions. – Describes all the needed information to run a method, including current method stack as well as other JVM shared spaces● Stack Frame registers (one per method being executed) – Local Vars, used by current method invocation – Frame data / Execution environment, used for internal stack operations – Optop / Operand stack (for bytecode), reference to runtime constant pool ● It is a LIFO stack, need push/pop operations http://performeister.tistory.com/38
  5. 5. JVM Heap● Two heap memory types ● Other memory – Young Generation Memory – Permanent Generation ● To/From Survivor spaces – Native memory (because of ● Eden space native memory references, – Old Generation Memory to the underlying OS)
  6. 6. Garbage collection● Principle: Generational collection or “weak generational hypothesis” – Most allocated objects are not referenced (considered live) for long, that is, they die young – Few references from older to younger objects exist● Minor GC – Young generation collections occur relatively frequently and are efficient and fast because the young generation space is usually small and likely to contain a lot of objects that are no longer referenced.● Major GC – Objects that survive some number of young generation collections are eventually promoted, or tenured, to the old generation http://www.oracle.com/technetwork/java/javase/tech/memorymanagement-whitepaper-1-150020.pdf
  7. 7. Young/Old Memory explained● Young Space – Eden ● Any object created inside a method or as a class variable – 2 survivor spaces (From/To) ● Objects in use, that have survived at least one Minor GC ● 1 space empty, other full. Swaps on every GC pass● Old (Tenured) – Objects referenced since a long time ago, still being used – Very big objects are created directly in Old space http://java-espresso.blogspot.ca/2011/05/heap-structure-in-jvm.html
  8. 8. GC example● Serial Collector (not currently used in modern JVM) http://www.oracle.com/technetwork/java/javase/tech/memorymanagement-whitepaper-1-150020.pdf
  9. 9. Parallel and CMS GC● CMS is the most used GC implementation
  10. 10. Heap fragmentation● We need contiguous space for memory allocation● GC also relocates (called compactation) objects to reduce fragmentation
  11. 11. G1 collector (JDK 6 & 7)● Garbage-First (G1) collector: server-style garbage collector, targeted for multi-processor machines with large memories.● It meets garbage collection (GC) pause time goals with a high probability, while achieving high throughput. Designed for applications that: – Can operate concurrently with applications threads like the CMS collector. – Compact free space without lengthy GC induced pause times. – Need more predictable GC pause durations. – Do not want to sacrifice a lot of throughput performance. – Do not require a much larger Java heap.● G1 is planned as the long term replacement for the Concurrent Mark-Sweep Collector (CMS).● Better than CMS. – G1 is a compacting collector. G1 compacts sufficiently to completely avoid the use of fine-grained free lists for allocation, and instead relies on regions. This considerably simplifies parts of the collector, and mostly eliminates potential fragmentation issues. – G1 offers more predictable garbage collection pauses than the CMS collector, and allows users to specify desired pause targets. http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/G1GettingStarted/index.html
  12. 12. G1 Heap Fragmentation Young GC
  13. 13. Heap evolution ● Regular case ● OOM caseTool: jconsole
  14. 14. Heap evolution ● Regular case ● OOM caseTool: websphere resource analyzer