UnderstandingGarbage Collection(aka Automatic Memory Management)
Douglas Q. Hawkinshttp://[email protected]
HotSpot Collectors
Young
Old
-XX:+UseSerialGC
-XX:+UseParNewGC -XX:+UseParallelGC
-XX:+UseConcMarkSweepGC -XX:+UseParallelOldGC
Serial ParallelScavenge
Serial Old(Mark Compact)
Concurrent Mark & Sweep
ParallelOld
(Mark Compact)
Parallel
TopicsBasic Garbage Collection Strategies & Terminology
Garbage Collection in HotSpot
Garbage Collection in J9 and JRockit
Alternatives to Garbage Collection
Azul C4
Tasks for a Tracing GCIdentify Live ObjectsRemove Dead ObjectsOptionally, Coalesce Free Space (Compaction)
Which Tracing Strategy?
Low(Young Region) Copy
High(Tenured Region)
Mark & SweepOR
Mark Compact
Liveness Strategy
Permanent GenerationYoung Generation
Old Generation
Eden
S1S0
Permanent Generation
Bootstrap App Server Web Application
C C C C C C C C
8xNewer 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
Maximize Collection during Minor GCsAvoid Major GCs at much as possible
Promote as little as possible
Maximize Available Memory
Adjust for 2 of 3 GC GoalsThroughput
LatencyFootprint
Basic Principles
Stops region resizingwhich would require a full GC
Set Xms = Xmxinitial heap size max heap size
3-4xYoung (Xmn): 1-1.5x
Old: 2-3x
GC ChoicesSerial ParallelVS.
Stop theWorld Concurrent
Compacting Non-compacting
VS.
Full VS. Incremental
Parallel MarkCompact
Application Thread GC Thread
ConcurrentMark & Sweep
Concurrent Marking
Remark
Concurrent Sweep
HotSpot Collectors
Young
Old
-XX:+UseSerialGC
-XX:+UseParNewGC -XX:+UseParallelGC
-XX:+UseConcMarkSweepGC -XX:+UseParallelOldGC
Serial ParallelScavenge
Serial Old(Mark Compact)
Concurrent Mark & Sweep
ParallelOld
(Mark Compact)
Parallel
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!
Azul C4Continuously Concurrent Compacting Collector
To From
http://www.infoq.com/articles/azul_gc_in_detail
SoftReference
SR
SoftReference<Foo> fooRef = new SoftReference<Foo>(foo);Foo foo = fooRef.get();
Garbage Collected if Low on Space
Be Careful With ReferencesReference<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());}
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
Additional Reading
The Garbage Collection HandbookBy Richard Jones, Antony Hoskin, Eliot Moss
http://www.amazon.com/The-Garbage-Collection-Handbook-Management/
Memory Management in theHotSpot Java Virtual Machinehttp://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-twitterBy Attila Szegedi
Everything I Ever Learned AboutJVM Performance Tuning
Top Related