Java
Virtual Machine provides four different algorithms for performing GC
1.Serial
Garbage Collector: The serial garbage
collector is the simplest of the four. This is the default collector if the application
is running on a client-class machine (32-bit JVMs on Windows or singleprocessor
machines). The serial collector uses a single thread to process the heap. It
will stop all application threads as the heap is processed (for either a minor
or full GC). During a full GC, it will fully compact the old generation. The serial collector
is enabled by using the -XX:+UseSerialGC
flag
(though usually it is the default in those cases where it might be used). Note
that unlike with most JVM flags,the serial collector is not disabled by
changing the plus sign to a minus sign (i.e., by specifying -XX:-UseSerialGC). On systems where
the serial collector is the default, it is disabled by specifying a different
GC algorithm.
2. The Throughput Collector: This
is the default collector for server-class machines (multi-CPU Unix machines,
and any 64-bit JVM). The throughput collector uses multiple threads to collect
the young generation, which makes minor GCs much faster than when the serial
collector is used. The throughput collector can use multiple threads to process
the old generation as well. That is the default behavior in JDK 7u4 and later
releases, and that behavior can be enabled in earlier JDK 7 JVMs by specifying
the -
XX:+UseParallelOldGC flag.
Because it uses multiple threads, the throughput collector is often called the
parallel collector. The throughput collector stops all application threads
during both minor and full GCs, and it fully compacts the old generation during
a full GC. Since it is the default in most situations where it would be used,
it needn’t be explicitly enabled. To enable it where necessary, use the flags -XX:+UseParallelGC
-XX:+UseParallelOldGC.
3.The CMS( Concurrent Mark and Sweep )
collector:
The CMS collector is designed to eliminate the long pauses associated with the
full GC cycles of the throughput and serial collectors. CMS stops all
application threads during a minor GC, which it also performs with multiple
threads. Notably, though, CMS uses a different algorithm to collect the young
generation (-XX:+UseParNewGC) than the throughput
collector uses (-XX:+UseParallelGC). Instead of
stopping the application threads during a full GC, CMS uses one or more background
threads to periodically scan through the old generation and discard unused objects.
This makes CMS a low-pause collector: application threads are only paused during
minor collections, and for some very short periods of time at certain points as
the background threads scan the old generation. The overall amount of time that
application threads are stopped is much less than with the throughput
collector. The trade-off here comes with increased CPU usage: there must be
adequate CPU available for the background GC thread(s) to scan the heap at the
same time the application threads are running. In addition, the background
threads do not perform any compaction, which means that the heap can become
fragmented. If the CMS back‐ground threads don’t get enough CPU to complete
their tasks, or if the heap becomes too fragmented to allocate an object, CMS
reverts to the behavior of the serial collector: it stops all application
threads in order to clean and compact the old generation using a single thread.
Then it begins its concurrent, background processing again (until, possibly, the
next time the heap becomes too fragmented). CMS is enabled by specifying the
flags -XX:+UseConcMarkSweepGC
-XX:+UseParNewGC (both
of which are false
by
default).
4. The G1( Garbage First ) collector: The G1 collector is designed to process large heaps (greater than about 4 GB)
with minimal pauses. It divides the heap into a number of regions, but it is
still a generational collector. Some number of those regions comprise the young
generation, and the young generation is still collected by stopping all
application threads and moving all objects that are alive into the old
generation or the survivor spaces. As in the other algorithms, this occurs
using multiple threads. G1 is a concurrent collector: the old generation is
processed by background threads that don’t need to stop the application threads
to perform most of their work. Because the old generation is divided into
regions, G1 can clean up objects from the old generation by copying from one
region into another, which means that it (at least partially) compacts the heap
during normal processing. Hence, a G1 heap is much less likely to be subject to
fragmentation—though that is still possible. Like CMS, the trade-off for
avoiding the full GC cycles is CPU time: the multiple background threads must
have CPU cycles available at the same time the application threads are running.
G1 is enabled by specifying the flag -XX:+UseG1GC (which by default is false).
No comments:
Post a Comment