Java Garbage Collection

  • You can request JVM to free up some memory which is no longer in use by garbage collection.
  • it is a process of freeing up memory from heap .

Making object garbage eligible:

example : Car carObj= new Car();

  1. nulling the reference eg: carObj=null;
  2. assigning a reference to another:
    • eg: Car carobj2= new Car() ; carObj1 ==carObj2
  3. By anonymous object etc. eg. new carObj();

Garbage collection in java:

In above code, System.gc() will make request JVM to run garbage collection.

The finalize() method is invoked each time before the object is garbage collected.

Output:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top