Taming Java Garbage Collector


Published on

These slides may not be self explanatory. Updated for the benefit of attendees of Colombo Java Meetup keynote.

Published in: Technology, News & Politics
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Taming Java Garbage Collector

  1. 1. Taming The Java garbage ColleCTor Daya Atapattu
  2. 2. Why Learn Garbage Collection? To select a GC algorithm To performance debug applications To become a better programmer
  3. 3. Summary How Java garbage collectors work How to monitor and tune GC How to write GC friendly Java code
  4. 4. Dynamic Memory Allocation Fortran77: No Dynamic Memory C: Dynamic Memory managed by programmer. malloc() / free() Java: Dynamic Memory managed by JVM, i.e. Java GC
  5. 5. Weak Generational Hypothesis Most objects die young. Only few references from old objects to young objects.
  6. 6. All Apples in One Basket?
  7. 7. GC Regions
  8. 8. Young Generation Collection
  9. 9. Old Gen and Compaction
  10. 10. Stop The World (STW)
  11. 11. Concurrent Mark-Sweep (CMS) Old gen is collected concurrently 1. STW, Mark roots 2. Mark concurrently with application 3. STW, mark again to catch up 4. Sweep
  12. 12. CMS
  13. 13. CMS Pros and Cons Reduces pause Does not collect all garbage. Leaves the heap fragmented so allocation is more expensive. CMS failures. Needs more heap. Steals threads and CPU from application.
  14. 14. G1 Collector
  15. 15. GC Tuning Use GC logging options with JVM -Xloggc:gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamp
  16. 16. GC Tuning 6.255: [GC [PSYoungGen: 132532K-16412K(278912K)] 141408K->25289K(453696K), 0.013 secs] [Times: user=0.08 sys=0.01, real=0.01 secs] 6.269: [Full GC (System) [PSYoungGen: 16412K->0K(278912K)] [PSOldGen: 8876K->24956K(174784K)] 25289K->24956K(453696K) [PSPermGen: 60341K->60341K(120960K)], 0.153 secs] [Times: user=0.16 sys=0.00, real=0.16 secs]
  17. 17. GC Monitoring Use tools to visualize GC activity Some such tools: JConsole JStat VisualVM (with GC extension)
  18. 18. Some Important Parameters GC - The garbage collector in use. Size of the Java heap. Size of the young and old generation spaces. Duration of minor garbage collections. Frequency of minor garbage collections. Duration of full garbage collections. Frequency of full garbage collections. What initiates full GC; old gen or perm space.
  19. 19. Attributes of Java Performance Pause Time: Minimal pause time due to GC Throughput: Overall performance of user process ignoring pauses due to GC Footprint: Amount of memory required for GC to run efficiently
  20. 20. GC Friendly Coding GCs love small, immutable, short-lived objects Object pooling is usually bad Large objects are expensive to allocate, expensive to initialize and may fragment CMS Nulling references is usually bad Avoid finalize() Avoid calling System.gc() Avoid memory Leaks Some sources: Objects in the wrong scope Instances of inner classes
  21. 21. Thank You! & Questions?