The benefit of the Metronome Garbage Collector is that the time it takes is more predictable and garbage collection can take place at set intervals over a period of time.
The key difference between Metronome garbage collection and standard garbage collection is that Metronome garbage collection occurs in small interruptible steps but standard garbage collection stops the application while it marks and collects garbage.
You can control garbage collection with the Metronome Garbage Collector using the -Xgc:targetUtilization=N option to limit the amount of CPU used by the Garbage Collector.
java -Xgcpolicy:metronome -Xgc:targetUtilization=80 yourApplication
The
example specifies that your application runs for 80% in every 60ms.
The remaining 20% of the time is used for garbage collection. The
Metronome Garbage Collector guarantees utilization levels provided
that it has been given sufficient resources. Garbage collection begins
when the amount of free space in the heap falls below a dynamically
determined threshold.The Metronome Garbage Collector consists of two types of threads: a single alarm thread, and a number of collection (GC) threads. By default, GC uses one thread for each logical active processor available to the operating system. This enables the most efficient parallel processing during GC cycles. A GC cycle means the time between GC being triggered and the completion of freeing garbage. Depending on the Java heap size, the elapsed time for a complete GC cycle could be several seconds. A GC cycle usually contains hundreds of GC quanta. These quanta are the very short pauses to application code, typically lasting 3 milliseconds. Use -verbose:gc to get summary reports of cycles and quanta. For more information, see: Using verbose:gc information. You can set the number of GC threads for the JVM using the -Xgcthreads option.
You cannot change the number of alarm threads for the JVM.
The Metronome Garbage Collector periodically checks the JVM to see if the heap memory has sufficient free space. When the amount of free space falls below the limit, the Metronome Garbage Collector triggers the JVM to start garbage collection.
After the garbage collection cycle has completed, the Metronome Garbage Collector checks the amount of free heap space. If there is still insufficient free heap space, another garbage collection cycle is started using the same trigger id. If there is sufficient free heap space, the trigger ends and the garbage collection threads are stopped. The alarm thread continues to monitor the free heap space and will trigger another garbage collection cycle when it is required.