In general ( as I am aware that there is a standard JVM implementation from Oracle/sun and other third parties as well like MS) , Does JVM create only one Garbage collection thread running as daemon to collect the garbage objects OR does JVM spawn more than one thread to accomplish the Garbage collection?
Call System.gc() anywhere in their code to instruct the JVM to prioritize garbage collection. When a developer calls this method -- and there isn't an extreme load on the JVM -- a Java GC cycle will happen within seconds.
Parallel Garbage Collector. It's the default GC of the JVM, and sometimes called Throughput Collectors. Unlike Serial Garbage Collector, it uses multiple threads for managing heap space, but it also freezes other application threads while performing GC.
The "throughput collector" which is enabled with -XX:+UseParallelGC
and is the default collector uses multiple threads. The "concurrent low pause collector" enabled with -XX:+UseConcMarkSweepGC
uses one thread for concurrent collector but its stop-the-world collections are parallel.
Only the rarely used single threaded gc -XX:+UseSerialGC
is single threaded.
http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html
http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html
-XX:ConcGCThreads=n -- Number of threads concurrent garbage collectors will use. The default value varies with the platform on which the JVM is running.
http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html#G1Options
There could be more garbage collector threads but you should not rely on their number, running sequence or anything. There are essential things however, you can rely on. for example: Object.finalize() will be called once and only once.
Also check out Tuning garbage collector, about the question:
http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html#1.1.%20Types%20of%20Collectors%7Coutline
Oracle's Garbage-First GC algorithm (available in Java 8 and the default option in Java 9) is a parallel/concurrent GC algorithm, so there is more than one thread involved. Specifically, there are a number of threads used for garbage collection:
G1GC can be enabled by setting -XX:+UseG1GC
(note that while available in Java 7 G1GC at that point in time was unreliable so don't use it in production (and since Java 7 has been end-of-lifed you shouldn't use that in production either)).
sources https://blogs.oracle.com/g1gc/entry/g1gc_faq and http://www.oracle.com/technetwork/articles/java/g1gc-1984535.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With