Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Finding your garbage

Finding your garbage

CMS and G1 garbage collection algorithms walkthrough

Dmitry Buzdin

February 01, 2017
Tweet

More Decks by Dmitry Buzdin

Other Decks in Technology

Transcript

  1. Summary • Intro to JVM Memory Areas • Intro to

    GC principles • Introducing CMS and G1 • Configuration possibilities • Measuring GC
  2. Memory Management • In languages like C++ you have to

    free memory • JVM cleans garbage after you • It does not mean that you can do whatever you want
  3. Common Memory Problems • Memory Leaks • “Stop the world”

    pauses • High CPU consumption by GC • Memory fragmentation
  4. Why do you need to know GC? • Understand whats

    going on under the hood • Interpret monitoring metrics • Understand how to adapt your code to JVM • Don’t optimise prematurely!
  5. GC Roots • Local variables of currently executed methods •

    Active threads • Static field of loaded classes
  6. Ideal GC Algorithm • Predictable pauses • Low latency •

    Supports large heaps • Not CPU intensive • Uses memory efficiently
  7. CMS Algorithm • Mostly Concurrent mark and sweep • Designed

    to avoid long pauses -XX:+UseConcMarkSweepGC
  8. CMS Collection Steps • Initial mark (STW) - collect all

    GC roots • Concurrent mark - mark all live objects in old gen • Concurrent preclean - mark dirty live objects • Concurrent abortable preclean - continuation of above • Final remark (STW) - finalize marking of live objects • Concurrent sweep - reclaiming space concurrently • Concurrent reset - reset internal data structures
  9. CMS in JDK9 • CMS is deprecated and to be

    removed in JDK 10? • Risk is that it outperforms G1 in some cases http://openjdk.java.net/jeps/291
  10. G1 Algoritm • Garbage First • Lots of small regions

    instead of large ones • Predictable “stop-the-world” pauses • Large heap sizes with low latency
  11. Initial Marking Phase G1 GC marks the roots during the

    initial-mark phase. Happens with Evacuation Pause
  12. Concurrent Marking Phase G1 GC looks for reachable objects across

    the entire heap. This phase happens concurrently.
  13. Configuring G1 • -XX:MaxGCPauseMillis (realistic soft limitation) • -XX:ParallelGCThreads •

    -XX:ConcGCThreads • -XX:InitiatingHeapOccupancyPercent • and more
  14. G1 Recap • Based on similar concepts as CMS •

    Additional dimensions with RSets and CSets • Support for humongous objects
  15. There is no best GC algorithm • Tradeoff between •

    Throughput • Latency • Heap size
  16. How to find the best one for your project •

    Experiment • Try different settings • Observe metrics • Understand the effect • Try again…
  17. Java GC Future • Further focus on G1 and improvements

    • Shenandoah https://rkennke.wordpress.com/