Quick introduction to Java Garbage Collector (JVM GC)
-
Upload
marcos-garcia -
Category
Technology
-
view
1.412 -
download
4
Transcript of Quick introduction to Java Garbage Collector (JVM GC)
JVM and Garbage Collection
by [email protected], Jan 2013
JVM?
Bytecode interpreter
http://viralpatel.net/blogs/java-virtual-machine-an-inside-story/
JVM Memory Vocabulary
Runtime data areasPC register: One for each thread
Points to current execution code of each thread
JVM stacksOne dedicated stack for each thread
Holds many stack frames, one for each invoked methodEvery frame has 3 registers
HeapShared among all threads
Stores Java objects.
Cleaned up by GC
Method areaShared 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
JVM Stack
Stack FrameUsed 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 poolIt is a LIFO stack, need push/pop operations
http://performeister.tistory.com/38
JVM Heap
Two heap memory typesYoung Generation MemoryTo/From Survivor spaces
Eden space
Old Generation Memory
Other memoryPermanent Generation
Native memory (because of native memory references, to the underlying OS)
Garbage collection
Principle: Generational collection or weak generational hypothesisMost allocated objects are not referenced (considered live) for long, that is, they die young
Few references from older to younger objects exist
Minor GCYoung 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 GCObjects 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
Young/Old Memory explained
Young SpaceEdenAny 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
GC example
Serial Collector (not currently used in modern JVM)
http://www.oracle.com/technetwork/java/javase/tech/memorymanagement-whitepaper-1-150020.pdf
Parallel and CMS GC
CMS is the most used GC implementation
Heap fragmentation
We need contiguous space for memory allocation
GC also relocates (called compactation) objects to reduce fragmentation
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
G1 Heap Fragmentation
YoungGC
Heap evolution
Regular case
OOM case
Tool: jconsole
Heap evolution
Regular case
OOM case
Tool: websphere resource analyzer