Java Major and Minor Garbage Collections

后端 未结 2 1197
不思量自难忘°
不思量自难忘° 2020-12-07 21:33

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 examp

相关标签:
2条回答
  • 2020-12-07 21:43

    While goblin's answer is still correct in broad strokes at least this part is now outdated:

    It depends. JVM reports every Major Collection as Full GC.

    Both CMS and G1 distinguish between new generation (minor) collections, concurrent collections of the old generation and full gcs. The latter are something of a last-resort thing and most of the GCing should be handled by new gen and concurrent collections.

    0 讨论(0)
  • 2020-12-07 21:58

    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
    0 讨论(0)
提交回复
热议问题