Skip to main content

Garbage Collection in Java

Garbage Collection : Destroying Object in Java Programming

in java, garbage means unreferenced objects.


Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects.
To do so, we were using free() function in C language and delete() in C++. But, in java it is performed automatically. So, java provides better memory management.

Advantages of Garbage Collection :


    1. It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory.
    2. It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.

      How can an object be unreferenced ?

      There are many ways:
      • By nulling the reference
      • By assigning a reference to another
      • By annonymous object etc.

        Garbage value doesn't exist in heap


        Garbage value exist in heap

      Comments