Monday, March 11, 2019

Garbage collection



Terms:
  • Young generation (Minor GC). 
    • Eden: 
      • New objects gets allocated here. 
    • From survivor
      • Once eden space is full, minor gb occurs and moves from from to to survivor. 
    • To Survivor
  • Old generation (Tenured) (Major GC) 
    • Oldest objects to old generation
  • Perm generation 
Note: 
  • SerialGen operates on the young gen. 
  • Concurrent gen operates on the old gen. 
-XX:+PrintClmmandLineFlags -version TestSystemGC

Concepts: 
Minor GC:

Garbage collection algorithms,
  • MinorJS: 
    • Serial Copy collector:  -XX:+UseSerialGC
      • -XX:+UseSerialGC
      • When to use: 
    • Parallell Scavenge Collector: -XX+UseParallelGC
    • Parallel copy collector: -XX:+UseParNewGC
    • Garbage first collector: -XX:+UseG1GC
  • Major JC:
    • Marksweep compact collector: -XX:+UseSerialGC
    • Parallell scavenge marksweep collector: -XX:+UseConcMarkSweepGC
    • Gargbage first collector: -XX:+UseG1GC

Full GC:
  • When full GC occurs, the Full GC pause time is normally lengthier. 

What you need to know
  • Frequency in which minor GC. 
    • Object allocation rate
    • Size of the eden space. 
  • Frequency of object promotion to old generation
    • Frequency of minor GC (how quickly objects age)
    • Size of survivor space. 
Steps: 
  • Check the frequency of Minor GC: (Defined by allocation rate and size of Eden)
    • Higher allocation rate and smaller eden space: More frequent minor GC
    • Lower allocation rate or larger eden space: less frequent minor GC. 
  • Check the frequency of Full GC: (Defined by promotion rate and size of old generation space)
    • For Parallel GC 
      • Higher promotion rate and small old generation space: frequent full GC
      • Lower promotion rate and larger old men: less frequent full GC.
    • For CMS & G1
  • G1: 
    • Avoids fragmentation. 
  • CMS: 

Notes:
  • The longer the object lives, the greater the impact on throughput latency and footprint
  • Object retention can degrade performance more than object allocation. 
  • GC visits only visits live objects. GCs love small immutable objects. 
  • Object allocation is very cheap (10 CPU instructions). 


Takeaways:
  • Its better to use short lived immutable objects vs long lived mutable objects. 
  • Start with -XX:+UseParallelOldGC and avoid full GC. 
  • If there are frequent full GC then move to CMS or G1 if needed (for old gen collections). 

Best practices: 
  • Avoid data structure resizing. 
  • Avoid large allocation. 
  • Dont do finalizers (requires GC cycles and GC cycles are slower), if required use reference objects as an alternatives. 
  • Soft references (don’t do it). 

No comments:

Post a Comment