When is full gc triggered?

Concepts

  • safe point: points where you can pause user thread,i.e., needed for GC
  • safe area: for sleep or blocked threads. In the code, reference relationship won’t change
  • Scavengc GC: triggered when Eden failed get more space. Eden region only
  • From OS pov, perm is in the jvm process’s HEAP.
  • Default max thread stack size is 1MB
  • Note that SWAP usage along with GC is a common source of problem
  • NIO’s memory is mostly in Kernal memory’s System area and PageCache area

GC roots

  • referred by local variables and operand stacks in jvm stack frame
  • method area, referred by class static
  • method area, referred by constant
  • local method stack, referred by JNI

Copying GC

  • Use only half of space, gc, and then move the survior into the other region, so that the other region has no frag
  • Often used by new gen,e.g., Serial, ParNew, Parrallel Scanvenge (at new gen), Serial old (for old gen), because new gen has less surviors and more garbages than older gen
  • New gen often uses ParNew

Compacting GC

  • move live objects toward one end of the heap. Note this idea is similar to Copying GC but NOT same!
  • may use a table of object handles, so that only handle -> actual actual address needs updated during copyign
  • Good for old gen where relativly less garbages exist,i.e., less need to move
  • Used in Mark-compact, which is common for old gens

Mark and Sweep GC

  • Separate of mark phase and sweep stage (and potentially compact stage to defrag), often used for old gen. Will stop the world and generate memory fragments
  • For each unreferenced objects, need to also mark if the finalizer has been run or not. The sweep phase will include finalization. Therefore, object may be resurrected by finalizer which makes the object reachable, and GC can not decide if change the object back to reachable to unreachable until after all resurrectable objects’ finalizers are run
  • Parallel Scanvenge at old gen, Parallel old,

Method area gc

Decide if constant and types are still in use. Need to satify ALL of

  • all instance are gced
  • the ClassLoader for this type is gced. Hence, types (not its instances!) loaded by the bootstrap classloader is never gced
  • java.lang.Class object for that type is not referenced/unreachable at all

Generational GC

The young generation is divided into Eden/From Survivor(S0)/To Survivor(S1). Size ratio 8:1:1, default to 1/15 of the heap size

  • The majority of newly created objects are located in the Eden space. Use copying GC
  • After a GC in the Eden space, the objects are piled up into the Survivor space, where other surviving objects already exist.
  • If (Eden + From Surivior) obj size is less than To Survivor space, move them to To Surivior, otherwise, move them to old gen directly. increase these To Survivor object age by 1, when it hits age 15, they enter old gen
  • After minor GC, if Eden is not enough to for the new object, the new object goes to old gen directly
  • To Surivior half size, and with equal age, any objects older than this age enter the old gen

  • when doing minor GC, will determin if old gen’s max continious space > total object sizes in the new gen, this is ONE of the steps of deciding if we should do full gc instead
  • For high throughput requirement, more likely to have bigger young gen and smaller old gens, so that most short term objects can be gced ASAP

Bump the pointer(BP) vs TLABs (Thread-Local Allocation Buffers)

  • BP just keep track of top pointer in Eden, and see if the new object can fit it in before updating the pointer
  • But in a multi-thread case, BP will cause lock contention. Therefore, TLAB gives each thread a small, thread-local space in Eden

Old gen obj refering to young gen

  • Use a 512 byte card table to record which old gen objects are referring to young gen
  • managed by a write barrier

Train algorithm

  • each gen divided into blocks of same size
  • each gc runs only on one block
  • for objects promoted to older gen, they are added to the tail of the older gen’s train