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. 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. 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. 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. 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. GC example
● Serial Collector (not currently used in modern JVM)
http://www.oracle.com/technetwork/java/javase/tech/memorymanagement-whitepaper-1-150020.pdf
10. Heap fragmentation
● We need contiguous space for memory allocation
● GC also relocates (called compactation) objects to reduce fragmentation
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