Garbage Collection

Why Garbage Collection in Java

Objective of Garbage collection is to free heap memory by destroying unreachable or unreferenced objects. Unreachable objects are the ones that are no longer referenced by any part of the program. Garbage Collector is best example of Daemon Thread.

Little About Unreachable Objects

As a programmer, you need not bother about garbage collection while doing coding/programming, it's better to know how and when objects become eligible for Garbage Collection:

  • Reference variable set to Null
  • Objects created inside method on method exit
  • Reassigning the reference variable to new ( previous object referenced is now eligible) 
  • Anonymous Object 
  • Island of Isolation ( Object O1 references O2 and O2 references O1 and neither of O1 or O2 is referenced by anyone else) 
It is important to note that any object becomes eligible for garbage collection only after each and every reference(s) to it become unreachable 

Garbage Collection Process 

Garbage Collection is a two-step process, Mark & Sweep.
Mark - Here Garbage Collections marks which objects are eligible for garbage collection.
Sweep - Actual Garbage Collection happens here for objects identified in above step.

Understanding Java Memory

Allocated Memory or Native Memory consists of :

  • Heap Memory
Heap Memory is further divided into :
      • Young Generation
      • Old or Tenured Generation
Young Generation again divided into:
          • Eden 
          • Survivor Space 
Survivor Space further divided into :
              • S0
              • S1

  • Stack 

Thread stack consists of :

      • Local Variables(Parameters & Variables)
      • Operand Stack ( Intermediate Results & Computation)
      • Frame Data ( Constant pool & return related Data)
  • Shared Libraries
  • Meta Space (Perm Gen Space on older Java versions)
Stores class definitions loaded by class loaders. When goes beyond allocated physical memory, it uses virtual Memory and may slow down application ( for transferring objects between virtual & Physical Memory)
  • Code Cache

Understanding Java Heap Memory

Let's start with both Young & Tenured Generation space empty. Now when objects are allocated memory , it will be allocated from Eden space. Now when Eden Memory reaches a Threshold, it triggers GC and dead objects will be garbage Collected and other objects moved to Survivor Space. Reason for having to Survivor Space is to avoid Memory Fragmentation. So initially, say Survived Objects were moved from Eden to So. After next Garbage Collection , Survived Objects from Eden will be moved to S1 . Now for objects which are already in S0, an age check would be run and if they are still young , they would be moved to S1 else they would be moved to Old generation or Tenured generation. Now So is again empty for objects to be moved from Eden in next minor GC.

Major GC is run ( threshold of Old Generation space or time threshold) , dead objects are collected and rest of them kept.

Few things to note :
  • Objects can be moved from young to tenured space in both minor and major GC. However major GC only collects dead objects in the tenured space.
  • If there are too many objects in Eden space, full GC is triggered and moves objects directly to old generation.
  • Age of object or number of times they are moved between S0 and S1 as a criteria for moving to Old Generation.
  • It makes sense to have Survivor Space > Eden otherwise there is more likelihood of Objects going from Eden to Old directly

Debugging/Configuring Options :

--XX :InitialTenuringThreshold
Default is 7,  number of times an object survives a young collection before being promoted to the old, or tenured, generation.

More Configuration options available here.

Different Garbage Collectors

  • Serial Garbage Collector
         Simplest one, Single Threaded, stops the world ( freezes application when it runs), not good for real time applications.
  -XX : UseSerialGC

  • Parallel Garbage Collector or Throughput GC
      Default JVM GC. Uses Multiple threads, freezes application.
-XX : UseParallelGC
 Here you can specify following parameters:
  1. No of Threads : -XXParallelGCThreads=N
  2. Pause time between GC : -XXMaxGCPauseMillis=N
  3. Head Memory Size(FootPrint) : -Xmx<N>
  4. Maximum Throughput Target : -XXGCTimeRation=N
where GCTimeRatio=Time Spent Inside GC/Time Spent outside GC

  • CMS Garbage Collector(Concurrent Mark Sweep)
   Multiple Threads,shorter GC Pauses, resources shared with GC when application is running .

  • G1 Garbage Collector ( Garbage First)
  For multi-processor machines, divides heap memory into regions, marks concurrently the objects and during sweep phase it knows which regions are most eligible (empty) for GC and it runs GC on them first , hence called as Garbage First.

No comments:

Post a Comment