Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JVM Garbage collection

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?

like image 205
java_mouse Avatar asked Jun 15 '12 13:06

java_mouse


People also ask

Can you force JVM to do 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.

What is the default JVM garbage collector?

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.


3 Answers

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

like image 60
Peter Lawrey Avatar answered Oct 06 '22 10:10

Peter Lawrey


-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

like image 41
illEatYourPuppies Avatar answered Oct 06 '22 10:10

illEatYourPuppies


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:

  • ParallelGC Threads are the threads that are used during a "stop the world" collection phase
  • Parallel Marking Threads (or Concurrent GC Threads) are threads used to mark regions as candidates for cleanup and run without stopping application threads
  • G1 concurrent refinement Threads are responsible for marking changes to individual region's remembered set of references

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

like image 42
rjohnston Avatar answered Oct 06 '22 09:10

rjohnston