Thursday 23 May 2019

What is GC , Heap Memory & Metaspace ?

What is GC (Garbage Collection)?
  • GC stands for “Garbage Collection”.
  • The JVM (Java Memory Management) has a feature of Garbage Collection,
  • The GC remove the unused objects from the program in order to make heap memory free.

The GC works in three steps format: “MARK”, “SWEEP” & “Deletion with Compacting”

  1. MARK: This step, mark the live objects in the heap memory.
  2. SWEEP: This step, removes (sweep) the dead/unreferenced objects from the heap memory.
  3. Deletion with Compacting: After removing unreferenced objects, this step compact the live/referenced objects together, which makes more memory allocation faster.

The Heap memory is divided into many generations:

1. Young Generation.
a.   Eden.
b.  Survivor (S0).
c.  Survivor (S1).
2. Old Generation (Tenured).
3. Permanent generation.

1. Young Generation.    
  • The (YG) young generations are the space where the new objects reside.
  • As young generations full, the Minor GC takes place.
  • Minor GC is always triggered when JVM is unable to allocate space for a new Object in Eden.
  • All Minor GC trigger Stop the world pauses, means stopping the application threads but mostly it is negligible.
  • Referenced objects are moved to the first survivor space (S0). Unreferenced objects are deleted when the Eden space is cleared. 

2. Old Generation (Tenured).
  • The (OG) Old Generation is the space where long survived objects reside.
  • Major GC is used to clean the Tenured space (OG). 

3. Permanent generation (PG).
  • The Permanent generation is also named as “Perm-Gen”, “Non-Heap Memory” and “Metaspace” space from JAVA 8.
  • Perm gen is used to store the class and methods objects, in the native memory which is used by the application.
  • During Full-GC the objects are cleared from Young - Old – Permanent.
  • This Perm gen is separated from main heap memory.

What is Metaspace ?

  • The metaspace is the new memory space. It has been replaced by “perm-gen” space.
  • Dynamic allocation of native memory space during class & memory leak.
  • GC calls, if the dead classes and classloaders reach the “MaxMetaspaceSize”.
  • The memory leaks can be identified by “java.lang.OutOfMemoryError “exception along with stack trace log.

  • java.lang.OutOfMemoryError: Java heap space
  • java.lang.OutOfMemoryError: GC Overhead limit exceeded
  • java.lang.OutOfMemoryError: Requested array size exceeds VM limit
  • java.lang.OutOfMemoryError: Metaspace
  • java.lang.OutOfMemoryError: request size bytes for reason. Out of swap space?
  • java.lang.OutOfMemoryError: Compressed class space
  • java.lang.OutOfMemoryError: reason stack_trace_with_native_method

Thanks :-)

No comments:

Post a Comment