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

Java Garbage Collector: Don't use the default

Avatar for Thiago Thiago
October 06, 2025
1

Java Garbage Collector: Don't use the default

Explore the inner workings of Java’s Garbage Collector and learn why sticking with the default may not always be the best choice. This talk breaks down how GC works, compares different collector types, and helps you choose the right one for your application’s performance needs.

Avatar for Thiago

Thiago

October 06, 2025
Tweet

Transcript

  1. Table of contents What is the Garbage Collector How the

    GC works JVM Ergonomics Is there more than one GC 01 02 03 04
  2. Memory Leak Memory Leaks are silent killers, slowly gobbling up

    system resources, until one day the entire application is fated.
  3. Garbage Collector Garbage collector, the silent hero of memory management,

    silently sweeps through the realm of allocated memory, freeing the forgotten fragments, and sparing developers from the shackles of manual memory deallocation.
  4. Throughput Throughput is a measure of how many tasks an

    application can simultaneously handle over a period of time.
  5. • Permanent Generation is intended for metadata that represents such

    as classes, methods… • Since Java 8, the name PermGen has been replaced by Metaspace; • Class metadata is deallocated when the corresponding Java class is unloaded. -XX:+MaxMetaspaceSize=100m Permanent Generation/Metaspace
  6. • Serial GC uses a single thread to run the

    GC; • Recommended for machines with 1 CPU; • Recommended for machines with up to 100MB; • Recommended for client-class machine applications; -XX:+UseSerialGC Serial GC
  7. • Parallel GC uses a multi thread to run the

    GC; • Recommended for applications with medium and large size; • Default GC for versions < 9; • Recommended for applications that focus on throughput; -XX:+UseParallelGC Parallel GC
  8. • Deprecated in Java 9 JEP 291 • Removed in

    Java 14 JEP 363 • It was replaced by the G1 Concurrent Mark Sweep GC (CMS)
  9. • G1 GC utiliza também multi thread para executar o

    GC; • Default GC para versões >= 9 JEP 248; • Limitar os pause times do GC é, em geral, mais importante do que maximizar o Throughput; • Recomendado para aplicações que buscam balancear a latência e o throughput; • Prove um equilibrio entre throughput e latency. -XX:+UseG1GC Garbage-First (G1) GC
  10. • ZGC é um GC projetado para baixa latência e

    alta escalabilidade. • Disponível para versões >= 15 JEP 377; • Não separa a heap em old e young generation; • Um dos objetivos do ZGC é ter pause times menores que 1ms. • Suportar aplicações pequenas (MBs) até aplicações muito grandes (TBs). -XX:+UseZGC ZGC
  11. Which GC will the JVM choose? A. Serial B. G1

    C. Depend D. ZGC E. Parallel
  12. Which GC will the JVM choose? A. Serial B. G1

    C. Depend D. ZGC E. Parallel
  13. Which GC will the JVM choose? If it has 2+

    processors and > 1792 MB it is considered as a server machine, and G1GC will be used.
  14. Which GC will the JVM choose? If it has up

    to 1791 MB it is considered as a client machine, and SerialGC will be used.
  15. • XX:MaxGCPauseMillis=200 • -XX:G1HeapWastePercent=5 - G1 stops the space-reclamation phase

    if the free space in the collection set candidates is lower than that. • Avoid explicitly setting young generation size with the -Xmn or -XX:NewRatio • If you want to change the young generation size use -XX:G1MaxNewSizePercent=60 or -XX:G1NewSizePercent=5 Garbage-First (G1) GC - Tuning