Java Virtual Machine Notes(2)

Garbage Collection(GC) in JVM

Determine Survive Objects

Actually, Java Virtual Machine doesn’t work as many sayings say — using
Reference Countng to decide whether an object should be recycled or not.
Instead, they use Reachability Analysis to determine an object’s lifetime.
It’s because Reference Counting cannot be used to solve some problems like
loop reference like

1
2
objA = objB;
objB = objA;

In this case, objA and objB cannot be recycled using Reference Counting
since their references counting is not 0. Instead, Reachability Analysis is
trying to find a path from GC Roots to current object. If there isn’t one,
then the object is marked as dead.

GC Algorithms

Kinds of ways are used in kinds of Java virtual machines. Different VM and
different version of machine use different algorithms to collect garbage.

  • Mark-Sweep: This algorithm is first to mark which objects are actually not
    reachable from GC roots and then recycle the momery occupied by these
    unreachable objects.
    • pros: easy to access
    • cons: stop the world while GC, fragment memory released
  • Copying: This algorithm is to split the total memory area into two equal
    parts at very beginning and each time just use one of it. When VM needs to
    collect garbage then copy the used one into another blank one and clear the used
    one.
    • pros: concurrent with users’ jobs
    • cons: reduce the actual using memory by half
  • Mark-Compact: This algorithm is first to mark which objects are actually
    reachable and try to move them into one end of the memory area of VM. After all
    alive objects are moved to one end then clear the other end of VM to release
    space.
    • pros: sort out memory space
    • cons: stop the world
  • Generational Collection: This algorithm is trying to partition VM momery into
    several parts based on resident objects’ ages. Each part should be applied corresponding
    algorithm.
    • pros: apply appropriate algorithm on different generations and can reach a
      good result
    • cons: More difficult to implement

Actually, during GC, VM doesn’t check every line to calculate reachability.
Instead, VM use a specific data structure to record which line changes the
reference among objects. Obviously this data structure is not equipped for every
line. They are recorded for some specific lines, which are also called safe
point
. GC starts when program comes to these safe points.