Reputation: 2109
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?
Upvotes: 7
Views: 2898
Reputation: 7363
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
Upvotes: 1
Reputation: 631
-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:
Upvotes: 1
Reputation: 533500
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
Upvotes: 6