This document discusses garbage collection and automatic memory management in Java. It covers the basic strategies used in garbage collection like mark and sweep. It also discusses the different garbage collectors used in HotSpot like SerialGC, ParallelGC, ConcMarkSweepGC and G1. It provides an overview of garbage collection in J9 and JRockit as well. The document also touches on alternatives to garbage collection like weak and soft references.
26. Tuning
Newer JVMs can adaptively tune,
but you may not want to rely on that.
Steady State
27. 8x
Newer JVMs can automatically tune,
but you may not want to rely on that.
-XX:MaxGCPauseMillis
-XX:GCTimeRatio
Meets these goals by adjusting the heap size.
Adaptive Tuning
28. Maximize Collection during Minor GCs
Avoid Major GCs at much as possible
Promote as little as possible
Maximize Available Memory
Adjust for 2 of 3 GC Goals
Throughput
Latency
Footprint
Basic Principles
29. Stops region resizing
which would require a full GC
Set Xms = Xmxinitial heap size max heap size
3-4x
Young (Xmn): 1-1.5x
Old: 2-3x
38. If Latency is Too High,
Switch to ConcMarkSweep
CMS kicks in when old generation is 65% full.
Increase Old Generation an additional 25-30%
When compaction is needed it is serial,
so avoid compaction at all costs!
52. Be Careful With References
Reference<User> ref = ...
if ( ref.get() != null ) {
System.out.println(ref.get().getName());
}
Possible NullPointerException
Reference<User> ref = ...
User user = ref.get();
if ( user != null ) {
System.out.println(user.getName());
}
53. ConcurrentMap<Key, Graph> graphs = new MapMaker()
.concurrencyLevel(4)
.weakKeys()
.maximumSize(10000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.makeComputingMap(
new Function<Key, Graph>() {
public Graph apply(Key key) {
return createExpensiveGraph(key);
}
});
Guava MapMaker & CacheBuilder
56. Additional Reading
The Garbage Collection Handbook
By Richard Jones, Antony Hoskin, Eliot Moss
http://www.amazon.com/The-Garbage-Collection-Handbook-Management/
Memory Management in the
HotSpot Java Virtual Machine
http://java.sun.com/j2se/reference/whitepapers/memorymanagement_whitepaper.pdf
By Charlie Hunt and Binu John
Java Performance
http://www.amazon.com/Java-Performance-Charlie-Hunt/
http://www.infoq.com/presentations/JVM-Performance-Tuning-twitter
By Attila Szegedi
Everything I Ever Learned About
JVM Performance Tuning