Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Major and Minor Garbage Collections

I have been reading up on Garbage Collection in Java and SO Q&A but I'm confused about types of Garbage Collection.

Let's take Throughput Collector as an example. (aka Parallel Collector). The docs say it uses multiple threads to do the Minor collections and single thread for Major collections (same as Serial collector).

Now my questions:

  1. What does it mean by a Full GC: a) Does it mean both Minor and Major collections are done? Or b) Full GC == Major Collections? Which one is it?
  2. If a), does it mean that the Minor Collection is still done using multiple threads whereas the Major was done using Single?
  3. If b), does it mean both Young & Old Generations were cleared using Single Thread?

Also, 4. Does Full GC only affect OldGeneration or YoungGeneration as well?

Thanks in advance.


1 Answers

Let me explain.

Let's take Throughput Collector as an example. (aka Parallel Collector). The docs say it uses multiple threads to do the Minor collections and single thread for Major collections (same as Serial collector).

Here's something to understand. By default, on most newer systems, JVM uses TWO different Garbage Collectors for Young and Old Generations. On my my machine: I have Parallel New Collector for the Young Generation and Concurrent Mark and Sweep Collector for the Older Generation.

Minor Collection is triggered when then JVM is unable to allocate space for a new Object (Remember: new objects are always allocated in Young Generation's Eden Area).

Next Question:

What does it mean by a Full GC: a) Does it mean both Minor and Major collections are done? Or b) Full GC == Major Collections? Which one is it?

and,

Also, 4. Does Full GC only affect OldGeneration or YoungGeneration as well?

It depends. JVM reports every Major Collection as Full GC. [Try with these flags java -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamp]. The pedantic definition is that Full GC runs Minor first followed by Major (Although the order could be switched if the Older Generation is full in which case it is freed first to allow it to receive objects from the Young Generation).

OK, back to the point. JVM considers Major Collection [in the Older (or Perm) Generation] as Full GC. Below are outputs from a program I was able to quickly write to illustrate the point. The first line is Minor GC and the second is Major (Full) GC. You can see that it only happened in the Older Generation (CMS) and was able to reduce old generation from 1082K to 1034K.

  • 11.431: [GC 11.431: [ParNew: 1152K->128K(1152K), 0.0009893 secs] 2111K->1210K(6464K), 0.0010182 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
  • 17.882: [Full GC (System) 17.882: [CMS: 1082K->1034K(5312K), 0.0212614 secs] 2034K->1034K(6464K), [CMS Perm : 9426K->9410K(21248K)], 0.0213200 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]

Next question:

If a), does it mean that the Minor Collection is still done using multiple threads whereas the Major was done using Single?

Yes. See the beginning of my answer. Young and Older Generations are served by different Collectors. For Young Generation, you can use any one of the following:

  • -XX:+UseSerialGC
  • -XX:+UseParallelGC
  • -XX:+UseParNewGC

For Old Generation, the available choices are:

  • -XX:+UseParallelOldGC
  • -XX:+UseConcMarkSweepGC
like image 161
goblinjuice Avatar answered Sep 13 '25 13:09

goblinjuice