Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Understanding
Garbage Collection
(aka Automatic Memory Management)
Douglas Q. Hawkins
http://www.dougqh.net
dougqh@gmail.c...
Why?
Leaky Abstraction
2 of 3 Optimization Flags Are For
Memory Management
but not about
tuning per se
...
...
HotSpot Collectors
Young
Old
-XX:+UseSerialGC
-XX:+UseParNewGC -XX:+UseParallelGC
-XX:+UseConcMarkSweepGC -XX:+UseParallel...
Topics
Basic Garbage Collection Strategies & Terminology
Garbage Collection in HotSpot
Garbage Collection in J9 and JRocki...
Fundamentals
Tasks for a Tracing GC
Identify Live Objects
Remove Dead Objects
Optionally, Coalesce Free Space
(Compaction)
Stack Frames
Mark & Sweep
Static Variables
System.in
System.out
Mark Compact
Allocation
4
8
Copy Collector
Tracing Problem
Random Walk of Heap
Cache Hostile
Bad Idea on Large Heap
Generational HypothesisBytesSurviving
Bytes Allocated
Infants
Generational
Young
Old / Tenured
Stack Frames
Static Variables
System.in
System.out
Remembered Set
Young
Old
Stack Frames
Static Variables
System.in
System.out
GC in
Java VMs
HotSpot Garbage Collector
Young Generation
Old Generation
Eden
S1S0
Young vs Tenured CollectionsBytesSurviving
Bytes Allocated
Young Tenured
Which Tracing Strategy?
Low
(Young Region)
Copy
High
(Tenured Region)
Mark & Sweep
OR
Mark Compact
Liveness Strategy
Object Allocation
Young Generation
Old Generation
Eden
S1S0
TLAB
Minor Collection
Young Generation
Old Generation
Eden
S1S0
TLAB
1 2
Major Collection
Young Generation
Old Generation
Eden
S1S0
Permanent Generation
Young Generation
Old Generation
Eden
S1S0
Permanent Generation
Bootstrap App Server Web Application
C...
Demo
https://visualvm.dev.java.net/plugins.htmlhttps://visualvm.dev.java.net/
Tuning
Newer JVMs can adaptively tune,
but you may not want to rely on that.
Steady State
8x
Newer JVMs can automatically tune,
but you may not want to rely on that.
-XX:MaxGCPauseMillis
-XX:GCTimeRatio
Meets the...
Maximize Collection during Minor GCs
Avoid Major GCs at much as possible
Promote as little as possible
Maximize Available ...
Stops region resizing
which would require a full GC
Set Xms = Xmxinitial heap size max heap size
3-4x
Young (Xmn): 1-1.5x
...
Set XX:PermSize =
XX:MaxPermSize
Again, stops region resizing
which would require a full GC
1.2-1.5x
GC Choices
Serial ParallelVS.
Stop the
World Concurrent
Compacting Non-compacting
VS.
Full VS. Incremental
Application Thread GC Thread
ParallelSerial
Stop the World!
Why Stop the World?
0x0C
0x0C
0x0C
0x04
0x04
0x04
BJL
Parallel Mark
Compact
Application Thread GC Thread
Concurrent
Mark & Sweep
Concurrent
Marking
Remark
Concurrent
Sweep
HotSpot Collectors
Young
Old
-XX:+UseSerialGC
-XX:+UseParNewGC -XX:+UseParallelGC
-XX:+UseConcMarkSweepGC -XX:+UseParallel...
Application Thread GC Thread
Throughput vs Latency
If Latency is Too High,
Switch to ConcMarkSweep
CMS kicks in when old generation is 65% full.
Increase Old Generation an a...
GCs in Other
JVMs
J9
Nursery
Tenured
SurvivorAllocate
C
JRockit
Nursery
Tenured
Keep Area Survivor
C C
C
Hybrid Mark Sweep & Copy
To From
New GCs in
Java VMs
G1:Garbage First & Balanced
G1 & Balanced GC
Unused Young Old
G1 - Minor Collection
Unused Young Old
Unused Young Old
G1 - Major Collection
Azul C4
Continuously Concurrent Compacting Collector
To From
http://www.infoq.com/articles/azul_gc_in_detail
Alternatives to Hard
References & GC
WeakReference
WR
WeakReference<Foo> fooRef =
new WeakReference<Foo>(foo);
Foo foo = fooRef.get();
SoftReference
SR
SoftReference<Foo> fooRef =
new SoftReference<Foo>(foo);
Foo foo = fooRef.get();
Garbage Collected if Low...
Be Careful With References
Reference<User> ref = ...
if ( ref.get() != null ) {
	 System.out.println(ref.get().getName());...
ConcurrentMap<Key, Graph> graphs = new MapMaker()
.concurrencyLevel(4)
.weakKeys()
.maximumSize(10000)
.expireAfterWrite(1...
PhantomReference
ReferenceQueue
PR
X
Direct ByteBuffers
ByteBuffer buffer = ByteBuffer.allocateDirect(...);
Additional Reading
The Garbage Collection Handbook
By Richard Jones, Antony Hoskin, Eliot Moss
http://www.amazon.com/The-G...
Questions?
Thanks, please fill out your evaluations!
Douglas Q. Hawkins
http://www.dougqh.net
dougqh@gmail.com
Upcoming SlideShare
Loading in …5
×

Understanding Garbage Collection

6,538 views

Published on

Overview of Garbage Collection in Java - covers basic GC concepts, GC mechanics, and provides basic tuning guidelines

  • Be the first to comment

Understanding Garbage Collection

  1. 1. Understanding Garbage Collection (aka Automatic Memory Management) Douglas Q. Hawkins http://www.dougqh.net dougqh@gmail.com
  2. 2. Why?
  3. 3. Leaky Abstraction 2 of 3 Optimization Flags Are For Memory Management
  4. 4. but not about tuning per se ... ...
  5. 5. HotSpot Collectors Young Old -XX:+UseSerialGC -XX:+UseParNewGC -XX:+UseParallelGC -XX:+UseConcMarkSweepGC -XX:+UseParallelOldGC Serial Parallel Scavenge Serial Old (Mark Compact) Concurrent Mark & Sweep Parallel Old (Mark Compact) Parallel
  6. 6. Topics Basic Garbage Collection Strategies & Terminology Garbage Collection in HotSpot Garbage Collection in J9 and JRockit Alternatives to Garbage Collection Azul C4
  7. 7. Fundamentals
  8. 8. Tasks for a Tracing GC Identify Live Objects Remove Dead Objects Optionally, Coalesce Free Space (Compaction)
  9. 9. Stack Frames Mark & Sweep Static Variables System.in System.out
  10. 10. Mark Compact
  11. 11. Allocation 4 8
  12. 12. Copy Collector
  13. 13. Tracing Problem Random Walk of Heap Cache Hostile Bad Idea on Large Heap
  14. 14. Generational HypothesisBytesSurviving Bytes Allocated Infants
  15. 15. Generational Young Old / Tenured Stack Frames Static Variables System.in System.out
  16. 16. Remembered Set Young Old Stack Frames Static Variables System.in System.out
  17. 17. GC in Java VMs
  18. 18. HotSpot Garbage Collector Young Generation Old Generation Eden S1S0
  19. 19. Young vs Tenured CollectionsBytesSurviving Bytes Allocated Young Tenured
  20. 20. Which Tracing Strategy? Low (Young Region) Copy High (Tenured Region) Mark & Sweep OR Mark Compact Liveness Strategy
  21. 21. Object Allocation Young Generation Old Generation Eden S1S0 TLAB
  22. 22. Minor Collection Young Generation Old Generation Eden S1S0 TLAB 1 2
  23. 23. Major Collection Young Generation Old Generation Eden S1S0
  24. 24. Permanent Generation Young Generation Old Generation Eden S1S0 Permanent Generation Bootstrap App Server Web Application C C C C C C C C
  25. 25. Demo https://visualvm.dev.java.net/plugins.htmlhttps://visualvm.dev.java.net/
  26. 26. Tuning Newer JVMs can adaptively tune, but you may not want to rely on that. Steady State
  27. 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. 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. 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
  30. 30. Set XX:PermSize = XX:MaxPermSize Again, stops region resizing which would require a full GC 1.2-1.5x
  31. 31. GC Choices Serial ParallelVS. Stop the World Concurrent Compacting Non-compacting VS. Full VS. Incremental
  32. 32. Application Thread GC Thread ParallelSerial Stop the World!
  33. 33. Why Stop the World? 0x0C 0x0C 0x0C 0x04 0x04 0x04
  34. 34. BJL
  35. 35. Parallel Mark Compact Application Thread GC Thread Concurrent Mark & Sweep Concurrent Marking Remark Concurrent Sweep
  36. 36. HotSpot Collectors Young Old -XX:+UseSerialGC -XX:+UseParNewGC -XX:+UseParallelGC -XX:+UseConcMarkSweepGC -XX:+UseParallelOldGC Serial Parallel Scavenge Serial Old (Mark Compact) Concurrent Mark & Sweep Parallel Old (Mark Compact) Parallel
  37. 37. Application Thread GC Thread Throughput vs Latency
  38. 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!
  39. 39. GCs in Other JVMs
  40. 40. J9 Nursery Tenured SurvivorAllocate C
  41. 41. JRockit Nursery Tenured Keep Area Survivor C C C
  42. 42. Hybrid Mark Sweep & Copy To From
  43. 43. New GCs in Java VMs
  44. 44. G1:Garbage First & Balanced
  45. 45. G1 & Balanced GC Unused Young Old
  46. 46. G1 - Minor Collection Unused Young Old
  47. 47. Unused Young Old G1 - Major Collection
  48. 48. Azul C4 Continuously Concurrent Compacting Collector To From http://www.infoq.com/articles/azul_gc_in_detail
  49. 49. Alternatives to Hard References & GC
  50. 50. WeakReference WR WeakReference<Foo> fooRef = new WeakReference<Foo>(foo); Foo foo = fooRef.get();
  51. 51. SoftReference SR SoftReference<Foo> fooRef = new SoftReference<Foo>(foo); Foo foo = fooRef.get(); Garbage Collected if Low on Space
  52. 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. 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
  54. 54. PhantomReference ReferenceQueue PR X
  55. 55. Direct ByteBuffers ByteBuffer buffer = ByteBuffer.allocateDirect(...);
  56. 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
  57. 57. Questions? Thanks, please fill out your evaluations! Douglas Q. Hawkins http://www.dougqh.net dougqh@gmail.com

×