Sobes.tech
Senior

How to specify a particular Garbage Collector in JVM (manually or by default)?

sobes.tech AI

Answer from AI

In JVM, the choice of garbage collector is set through command line parameters when launching the application. For example:

  • -XX:+UseSerialGC — use Serial Garbage Collector.
  • -XX:+UseParallelGC — Parallel Garbage Collector (default in some versions).
  • -XX:+UseConcMarkSweepGC — CMS (Concurrent Mark Sweep).
  • -XX:+UseG1GC — G1 Garbage Collector (often the default in modern JVM versions).
  • -XX:+UseZGC — Z Garbage Collector (starting from Java 11).
  • -XX:+UseShenandoahGC — Shenandoah (in some builds).

Example of launching with G1 GC:

java -XX:+UseG1GC -jar myapp.jar

If the parameter is not specified, JVM chooses the default garbage collector, which depends on the JVM version and platform.