Wednesday 12 April 2023

JVM - Garbage Collection - GC#001

 Garbage Collection - GC#1


Process of reclaiminh the runtime unused memory by destroying the un used objects

Methods like free() in C and deletete() in c++ are used to perform clean garbage.

In Java, GC is automatic during the life time of a program

At any point of RT, heap memory consistes of 2 types of objects:

Live : Being used and referenced

Dead : not used and not referenced currently.

How to derefernce objects:

By making a reference null { object o1 = null;}

By assigning a reference to another { object o1 = o2;}

By using an anonymous object { new Object;}


How GC Works:

       Many Versions of JVM. has its own implementation of GC .

       Any version of JVM can be used but should meet the std spec like :

: mark the unreachable objects

: Destroy them    

: Compacting the memory.


GC Roots: to identify the live and dead objects

Local vars and input paramas

        Active Threads

  Static Fields of the Loaded classes

JNI In(ref)ferences


It traverse the full object graph in mem and marks live objects(visited). ALl not reacheble(not visited) by grtaph are identified as dead objects.


Sweep Phase and compact phase ( Fragmented mem blocks are made as contiguos blocks)


Generational GC strategy:


Marking will take a longer GC Time. All objects are mostly short lived.

Heap : (eden, S1, S2)

        Young Generation : Newly 

-Eden Space : all new objects start here, and initial mem is allocated to them

-Survivor Spaces : (S to S)  - Objects are moved here from Eden after surviving one garbage collection cycle.

        When eden space if filled , aminor GC is performed and all the survivir objects are moved to one of the survivor spaces.

Minor GC checks also the survivor space. All the dead objects are reoved when Minir GC is performed.

Second time Minor GC, is used Eden and Survivor space. At any point of time Survivor space is empty.


        Old Generation : Long lived objects are eventually moved from the Young generation to the Old Generation.

This is also known as Tenured generation and conatins objects that have maintained in  the survivor spaces for a long time.

When objects are garbage collected here, it is called as majot garbage collection event.

Permenant Generation :

Metat data such as classes and methods are stored in Perm Genearation

Classes thar are no longer in use may be GC from the permenant generation.

Now Metaspace memory soppace replaced the permenant generation. Heap is now automatically resized. Avoids the the pbm of apps going out of memory due to the limited size of 

PermGen space of heap.

Classes that are no longer used is automatically cleaned when the metaspace searches its maximum size.


No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...