Mca I Yr. (Aiml) - Sem - I: Course Name: Object Oriented Programming
Mca I Yr. (Aiml) - Sem - I: Course Name: Object Oriented Programming
Mca I Yr. (Aiml) - Sem - I: Course Name: Object Oriented Programming
(AIML)- SEM – I
Even though the programmer is not responsible for destroying useless objects but
it is highly recommended to make an object unreachable(thus eligible for GC) if
it is no longer required.
There are generally four ways to make an object eligible for garbage collection.
1.Nullifying the reference variable
2.Re-assigning the reference variable
3.An object created inside the method
4.Island of Isolation
Ways for requesting JVM to run Garbage Collector
Once we make an object eligible for garbage collection, it may not destroy
immediately by the garbage collector. Whenever JVM runs the Garbage
Collector program, then only the object will be destroyed. But when JVM runs
Garbage Collector, we can not expect.
We can also request JVM to run Garbage Collector. There are two ways to do it :
• Using System.gc() method: System class contain static method gc() for requesting JVM
to run Garbage Collector.
• There is no guarantee that any of the above two methods will run Garbage Collector
Based on our requirement, we can override finalize() method for performing our
cleanup activities like closing connection from the database.
• The finalize() method is called by Garbage Collector, not JVM. However,
Garbage Collector is one of the modules of JVM.
• The finalize() method is never invoked more than once for any object.
•It makes java memory-efficient because the garbage collector removes the
unreferenced objects from heap memory.