Java provide automatic memory management through a program called Garbage Collector. Garbage Collection will be carried out by daemon thread called GarbageCollector.

In Java, Objects are allocated in the “heap” of Java memory where as static members, class definitions(metadata) etc., are stored in “method area” commonly known as PermgenMetaspace.

Remember, GarbageCollection cannot be forced, JVM itself will take care of it.

When new Objects are not able to allocate memory in the Heap throws java.lang.OutOfMemoryError

LifeCycle of Garbage Collection involves:

1. Mark
2. Delete / Sweep
3. Compact

Mark:
GarbageCollector
walks through the Object graph, and marks objects that are not reachable.

Delete / Sweep: 
Delete the unreachable objects, which frees memory and make it available for object creation.

Compact:
After deleting or sweeping, arrangement of live objects and free memory will be fragmented, it will be arranged in contiguous manner.

Generation of Objects in Heap

As Objects getting older, Objects will be sweep to one generation to another generation in the Heap.

Heap memory will have generations for maintaining the objects;
1) Young Generation
2) Old Generation.
3) PermGen Space (till the Java 8) / meta-space generation (From Java 8) .

PermGen stands for permanent generation which holds the meta-data information about the classes.

1. All instance variable will be stored in heap memory and classes along with static class loaders will be stored in permanent generation.

2. Garbage collectors will find it difficult to clear or free the memory space stored in permanent generation memory. Hence it is always recommended to keep the permgen memory settings to the advisable limit.

3. Java 8 has introduced the concept called Meta-Space generation, hence PermGen is no longer needed when you use jdk 1.8 version.

Metaspace garbage collection

 1. Meta spaces uses will not use JVM Memory !!
 2. Meta spaces uses OS memory and limit can be added using "MaxMetaspaceSize" option.
 3. Garbage collection of the dead classes and classloaders is triggered once the class metadata usage reaches the “MaxMetaspaceSize”.
How Generation changes for Java Object in the memory. ?

This is very interesting story about Java Object Generations.

Objects get birth in YoungGen and Resides in OldGen.

           When object is created it will reside in EdenSpace of YoungGen Area. In YoungGen MinorGC will take place when the EdenSpace reaches the threshold.

During MinorGC, GC Thread(s) will be marking the objects that are live or active as Reachable Objects, where object which are not used will be marked as Un-Reachable Objects, Un-Reachable objects will be sweep. Reachable Objects will be passed to Survivor space.

Eden Space : The pool from which memory is initially allocated for most objects. Survivor Space : The pool containing objects that have survived the garbage collection of the Eden space.
Minor GC and Major GC:

Collecting garbage from Young space (consisting of Eden and Survivor spaces) is called a Minor GC. 
Where as,
Collecting garbage from Tenure or OldGen space is known to as Major GC.

After many cycles of Minor GC, survival pool Objects is really getting older and will be moved to Old or Tenured pool.
This is the place where MajorGC will take place, when Tenure Pool is really crosses threshold limit, Garbage Collector kicks in to perform Mark, Sweep, Compact the generation pool sequentially, during this application will be paused !!!

Tenured Space: The pool containing objects that have existed for some time in the survivor space.

How to manage Application Pause time:

Java Provides various GC for concurrent actions, parallel actions, using the GC Threads efficiently in YoungGen and in OldGen, pause time can be reduced.

Thanks for Reading this Post !!!