Tuesday, January 29, 2008

Garbage Collector and finalize :

Garbage Collector and finalize :

Analogy:
The day when my room is dirty, I usually call my servant, name JVM, to clean it up. Servant JVM collects junk from every part of the house and collect all the stuff together, then throw them.

Normally the ways JVM cleans room are -

> If servant JVM is not very busy, It first collects all the objects such as paper, junk boxes etc and asks to me to check if there is anything important for me. I pick up whatever I think is usable, and then give permission to servant JVM to throw rest of stuff it collected.

> If the Servant JVM has lots of other things to do, It doesn't ask me to look into the collected junk stuff  and throws it considering that everything it collected is waste.
This is the concept behined "GARBAGE COLLECTION". Here -

Room == Classes in Java
Junk stuff == Objects without references
Servant JVM == Gabage Collector
Act of asking me to check the collected junk stuff == Finalize method.

For an Object reference:
Maximum number of time finalize can be called = 1
Minimum number of time finalize can be called = 0

Programmatic example  { Copyright (c) 2011 http://jovialjava.blogspot.com }

2 comments:

Anonymous said...

Dude you can actually join..Kathy and bert..!!!

This was an awesome explanation..!!
I am so surprised how garbage collection is similar to what happen around us..***YOU ROCKS** :D

Taufique.S.Shaikh said...

Explained in very great manner
this is the best way to explain garbage collection.