The CMS & G1.mdown—C:\Users\ye830\Desktop\GC

CMS and G1

What is GC?

  • Object life cycle auto management
  • Makes developer focus on the coding
  • Stack auto management



  • New Generation(Yong Generation)
    • Eden
    • Survivor(0/1)
  • Old Generation(Tenured Generation)
  • PerGen(Permanent Generation, Metaspace)

Garbage Collection way

  • Mark - Copy
  • Mark - Sweep(CMS)
  • Mark - Reorganize(G1)

GC Type

  • Serial(Default for client jvm)

    • Single thread
    • The stack size less equal 100M
    • -XX:+UseSerialGC
  • Throughput(Default for server jvm)

    • Will stop-the-world(时空停顿) during Minor GC and Full GC
    • Use paralle threads to collect the gabage
    • -XX:+UseParallelGC
  • CMS

    • Will stop-the-world(时空停顿) during Minor GC but not on Full GC
    • Use concurrent threads to collect the gabage on Old Generation
    • Will shotly stop-the-world(时空停顿) during scanning & marking time on Old Generation
    • Will down grade to Serial GC when the Old Generation is too fragmentation
    • -XX:+UseConcMarkSweepGC
  • G1

    • Have good performance on the super large stack(over 4GB)
    • Split the Old Generation to 2048 region
    • Consume the CPU
    • Because of the region, G1 nearly do not need to do the Full GC
    • -XX:+UseG1GC


  • Throughput



  • CMS



  • CMS Steps

    • YGC
    • Concurrent Cycle
      • Initial mark(stop-the-world, find all GC object)
      • Concurrent marking(Do not stop-the-world, just give the object a mark)
      • Concurrent preclean(Prepare to sweep)
      • Remarking
        • Concurrent abortable preclean()
      • Sweep
      • Concurrent reset
  • G1




  • G1 Steps
    • YGC
    • Concurrent Cycle
      • Initial makr(Find the G1, stop-the-world)
      • Root region scan
      • Concurrent marking(stop-the-world for a very shot time, just give the object a mark)
      • Remarking(stop-the-world for a very shot time)
      • Concurrent clean up
      • Mixed GC(Minor GC + clean the Old Generation region + collection active data into another region + start another Concurrent cycle)


  • The Code

    • Write the code as less as possible
    • Use the most suitable variable type
    • Reduce the unecessay new Object
    • Delayed loading
    • Clean as quick as possible(Null)
      • ArrayList(Removed)
      • ArrayList

    • Do not use System.gc() unless you really need the space and do not care about the time
  • The JVM configuration

    • -XX:ConcGCThreads
    • -XX:CMSInitiatingOccupancyFraction=N(Default is 70) + -XX:+UseCMSInitiatingOccupancyOnly(Default is false)
    • -XX:InitiatingHeapOccupancyPercent=N
    • -XX:G1MixedGCLiveThresholdPercent=N
    • --XX:G1ReservePercent=n


  • Concurrent mode failure

    • When YG prepare to sweep but no space for the OG
    • When YG prepare to sweep but OG is too fragment
  • Java Heap Space
    java.lang.OutOfMemoryError: Java heap space

  • Metaspace(PerGen Space)

  • GC overhead limit exceeded

The End

  • Can you put up with Full GC pause time?
  • Could default setting meet your requirement?
  • Did the pause time meet your expectation?
    • GC time pause time too long?
  • Conncurrent model failture?
  • Upgrade failture?

[1]:Java Performance: The Definitive Guide