Garbage Collection || Classes And Objects || Bcis Notes

Garbage Collection || Classes And Objects || Bcis Notes

Garbage Collection

In Java, garbage means unreferenced objects.

Garbage Collection is a 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 a free() function in C language and delete() in C++.


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


Unreachable objects: An object is said to be unreachable if it doesn’t contain any reference to it.

Integer i = new Integer(4);
// the new Integer object is reachable via the reference in ‘i’
i = null;
// the Integer object is no longer reachable.

Eligibility for garbage collection :
An object is said to be eligible for GC(garbage collection) if it is unreachable.

Steps Of Garbage Collection

  • Marking
  • Normal Delation
  • Delation With Compacting


The garbage collector determines which pieces of memory are in use and which are not.


Normal Delation

Garbage collector removes unreferenced objects leaving referenced objects and pointers to free space.


Delation With Compacting

Garbage collector deletes unreferenced objects and compacts the remaining referenced objects.


Garbage Collection || Classes And Objects || Bcis Notes


How can an object be unreferenced?
There are many ways:
– By nulling the reference
– By assigning a reference to another
– By anonymous object etc.


By nulling a reference:

Employee e=new Employee();


By assigning a reference to another:

Employee e1=new Employee();
Employee e2=new Employee();

//now the first object referred by e1 is available for garbage collection


By anonymous object:

new Employee();


The final keyword in java is used to restrict the user. The java final keyword can be used in many contexts. Final can be:

  • variable
  • methoD
  • class


class Bike9{
final int speedlimit=90;//final variable
void run(){
public static void main(String args[]){
Bike9 obj=new  Bike9();;
}//end of class

You may also like Operator Precedence || Java Programming Basics || Bcis Notes

Be the first to comment

Leave a Reply

Your email address will not be published.