You are on page 1of 1

Garbage Collection

Garbage collection is a process where the objects that are no longer reachable are freed from
memory. Not reachable object means that the object is no longer referenced by any variable.
That object is occupying space in memory unnecessarily. So when the program feels it is short
on memory it will check if there are any objects which can be destroyed. You cannot guarantee
when a garbage collector would run. It purely depends upon the program.

Although in java, you can request the garbage collection process to run but you cannot force it. It
is just a request and program will decide whether to run grabage collector or not. If you want to
perform any action before object is collected, do that in finalize() method. This method runs
whenever garbage collector runs for any object. But remember, finalize() method runs only once
for each object. If it ran once, it won't run again for the same object.

You might also like