An introduction into the Garbage First (G1) garbage collector for the JVM. The session covers general GC concepts, the fundamentals of G1 and how to setup and tune the JVM for G1.
2. Web/Mobile Developer since the late 1990s
Interested in: Java & JVM, CFML, Functional
Programming, Go, JS, Android, Raspberry Pi
!
And this is my view of the world…
Me
3.
4. 1.The JVM and Garbage Collection in 5 mins
2.Academic ideas behind G1
3.The G1 collector
4.Tuning G1 and practical implications
5. Further changes in Java 8
Agenda
6. Fundamentals
The most simplistic view of the JVM:
!
“Java virtual machine (JVM) interprets
compiled Java binary code (called bytecode)
for a computer’s processor (or hardware
platform) so that it can perform a Java
program's instructions.”
10. Java stack vs heap memory
Each method call creates a new stack frame,
which has an operand stack, array of local vars
and a program counter.
→ Seen ‘Stack Traces’ in a Java Error before?
!
Exception
in
thread
"main"
java.lang.NullPointerException
at
com.example.myproject.Book.getTitle(Book.java:16)
at
com.example.myproject.Author.getBookTitles(Author.java:25)
at
com.example.myproject.Bootstrap.main(Bootstrap.java:14)
13. Heap management
The JVM has no way of knowing the lifespan of a
certain object in advance.
Generational Memory Management is a solution
to overcome this issue and fragmentation:
-Young Generation
- Old Generation / Tenured Generation
- Sometimes: Permanent Generation
16. YG Collectors: Parallel
Parallel MaC (since Java 1.4.2) distributes the
Marking and Copying phases over multiple
threads.
The actual collection is still stop-the-world, but
for a much shorter period of time.
YG default since Java 5 if machine has 2+ cores
or CPUs, otherwise: -XX:+UseParallelGC.
!
17. OG Collectors: Concurrent
Up to Java 6/7 Concurrent Mark-and-Sweep is
the preferred OG collector if you want to
minimise stop-the-world collections.
CMS via -XX:+UseConcMarkSweepGC
Well suited for larger heaps (but be aware of
fragmentation), there’s an ‘incremental’ mode for
systems with 1-2 CPU cores.
Stop-the-world and concurrent collections
19. “Garbage-First Garbage Collection”
Research paper originally published in 2004 by
David Detlefs, Christine Flood, Steve Heller and
Tony Printezis of Sun Research.
!
The actual research project started in the late
1990s to overcome common issues in Garbage
Collection techniques known and used at the
time.
20. Core ideas
Four core elements:
- SATB concurrent marking algorithm
- Better way to achieve a real-time goal
- Get rid of a contiguous heap and use regions
- Compacting and predictable
21. Snapshot-at-the-beginning
SATB does a periodic analysis of global
reachability (liveness) and provide completeness.
Results:
- Accurate counts of live data in each region
- Completeness: garbage is eventually identified
-Very low pause time
!
22. ‘Soft’ real-time goal and regions
Before G1, garbage collectors tried to achieve
hard real time goals by:
- making collection interruptible
- working on the granularity of object levels.
G1 works on a coarser granularity of regions:
- Chooses regions to collect that match goal
- Collection of a region can be delayed
!
25. G1 (Garbage First)
G1 is a ‘replacement’ for CMS in Java 7+
Benefits:
- Consistently low-pause
- Adaptable
- Less fragmentation than CMS
- Less need for ongoing tuning
- Best collector for a really large heap
26. Fundamental ideas (I)
Minimum of 6 GB heap, if below - consider
staying with CMS
Enable: -XX:+UseG1GC
Provide minimal set of expectations and let G1
do the job:
- Heap size (min/max)
- How much CPU time can the application use?
- How much CPU time can G1 use?
27. Fundamental ideas (II)
‘Main’ setup parameter:
-XX:MaxGCPauseMillis=<n>
G1 is not an OG-only collector like CMS
G1 splits the whole area of heap memory:
- ~2000 regions
- Size between 1-32 MB each
- usually automatically chosen by the JVM
!
28. Region setup in G1
Note:There is another region type H (humongous)
30. AYG collection in G1 (before)
Non-Allocated
Old Generation
Young Generation
Recently copiedYG
31. AYG collection in G1 (stop-the-world)
Non-Allocated
Old Generation
Young Generation
Recently copiedYG
32. AYG collection in G1 (result)
Non-Allocated
Old Generation
Young Generation
Recently copiedYG
33. YG in G1 - Summary
TheYG is a set of non-contiguous regions, which
helps resizing after a collection.
YG collections in G1 are stop-the-world events
and all application threads will stop.
YG collections are done in multiple, parallel
threads.
Leftover (alive) objects → move to a survivor or
OG region.
35. G1 and the OG - overview (I)
1. Initial Mark (stop-the-world and piggybacking
on aYG collection)
2. Root Region Scan (blocksYG from happening)
3. Concurrent Marking
4. Remark (stop-the-world and due to a new
algorithm much faster than CMS)
36. G1 and the OG - overview (II)
5. Cleanup (stop-the-world and concurrent)
6. Copying (stop-the-world, piggybacking onYG
collections)
!
!
!
!
37. OG collection in G1(initial marking)
Marks root regions with references to OG objects.
Non-Allocated
Old Generation
Young Generation
Recently copiedYG
Recently copied OG
38. OG collection in G1(concurrent marking)
Marks empty regions and calculates object ‘liveness’.
X
X
Non-Allocated
Old Generation
Young Generation
Recently copiedYG
Recently copied OG
39. An OG collection in G1(remark)
Empty regions are removed and reclaimed.
Non-Allocated
Old Generation
Young Generation
Recently copiedYG
Recently copied OG
40. An OG collection in G1(cleanup & copy)
Region with lowest liveness get collected withYG
collections (‘mixed’ collections).
Non-Allocated
Old Generation
Young Generation
Recently copiedYG
Recently copied OG
41. An OG collection in G1(result)
Collection is done and leftovers are compacted.
Non-Allocated
Old Generation
Young Generation
Recently copiedYG
Recently copied OG
42. OG in G1 - Summary
Concurrent Marking:
- Liveness info determines where to collect
- No sweeping phase like in CMS
Remark:
- SATB algorithm much faster than CMS
- Completely empty regions are reclaimed
Cleanup: optimised for ‘mixed’ collections
44. Do not trust consultants, blog posts, mailing list
discussions etc. telling you what the ‘best’ JVM
settings would be.
!
There is no such thing as global best settings.
JVM settings depend on the environment, the
application and the projected/actual usage.
46. The two main tuning parameters for G1
-XX:MaxGCPauseMillis
Soft goal target for maximum GC pause time -
default 200ms
-XX:InitiatingHeapOccupancyPercent
Percentage of heap occupancy to start
concurrent GC cycle
47. Good practice
Avoid setting absolute generation sizes with G1:
- Breaks self-optimisation and target times
- Causes issues in region sizing & distribution
Avoid evacuation failures (‘space overflow’):
- Increase heap promotion ceiling (default 10)
-XX:G1ReservePercent
- Increase # of marking threads
-XX:ConcGCThreads
48. Real-world observations (I)
G1 has a noticeable tradeoff between latency
and throughput:
- G1: ~90-92% throughput goal
- Parallel Hotspot GC: ~98-99% goal
If you want higher throughput - relax the pause
time goal.
49. Real-world observations (II)
‘Mixed’ GCs are on the more expensive end in
G1.You can tamper with the criteria through
experimental settings*:
-XX:G1MixedGCLiveThresholdPercent
-XX:G1HeapWastePercent
!
* Might not be available on your platform, CPU
architecture or JVM version.
50. Real-world observations (III)
CPU usage tends to increase ~5-15% when using
G1 vs. CMS.
G1 seems to be better in reclaiming the
maximum heap sized used.
The more uniform your object size distribution
is, the better is CMS over G1.With a very
heterogenous object size distribution, G1 tends
to be better.
52. Most of the previous is
valid for Java 7 and 8 -
but…
53. G1 string de-duplication
Java 8u_20 brings a new String de-duplication
optimisation.
G1 collector can now identify strings that are
duplicated across the heap and repoint them to
the ‘same’ internal char[] representation:
-XX:+UseStringDeduplicationJVM
54. Java 8 and the PermGen
It’s gone and it’s been replaced by a Metaspace
(Oracle’s JRockit actually never had a PermGen).
Class metadata is now stored in native memory.
!
A word of warning: Oracle tries to sell the
Metaspace as the new piece of awesomeness
that ‘just works’, but it still needs observation
and tuning!
55. Retired JVM GC combinations
Some rarely-used combinations of garbage
collectors have been deprecated:
http://openjdk.java.net/jeps/173
!
Important: iCMS has been deprecated!
!
!
56. Additional Resources
Garbage Collection with Garbage First Research Paper:
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.63.6386&rep=rep1&type=pdf
Understanding G1 logs:
https://blogs.oracle.com/poonam/entry/understanding_g1_gc_logs
“The JVM is your friend” - my more general GC talk at cf.Objective() 2014:
http://www.slideshare.net/AgentK/jvm-isyourfriend
Java Performance:The Definitive Guide
http://www.amazon.com/Java-Performance-The-Definitive-Guide/dp/1449358454
!