Important alert: (current site time 7/15/2013 4:01:18 PM EDT)
|
|
|
|
|
Explains the concepts of Garbage Collection and object resurrection in java. I have taken a novel approach so do comment/vote, please.
|
| | Terms of Agreement:
By using this article, you agree to the following terms...
- You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
- You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
- You may link to this article from another website, but ONLY if it is not wrapped in a frame.
- You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
|
The Story Of Resurrection.
Once there lived an object of Criminal class, reffered as Rambo. he had many alias BigCat, BlackTiger, TheRock ...but they were all the same person.
He committed a serious crime for which he was to be removed from this world (the heap), all his Refrences and Alises where nulled from records (stack) so that ShootingSquard (System.gc()) could take him. Rambo knew that any Exception he may throw at the Squard will not save him since his hour has come. what did your ancestors do in their final hours ?? asked the squad, I dont know I have to recall it explicitly answered rambo.
As the traditions of the land (JVM), he was asked for any final wish before is death, the Squard promised to fulfill if in mood (?) , the cunning Rambo immediately asked for resurrection, bound by the promise the moody Squad let him go with a new Alias.
A few days later he was caught again for a similar crime, and was again awarded death , but this time no one asked his wish.
the code ! ! !
public class MainClass{
public static void main(String arg[]){
System.out.println("In the beginning there was .... \n");
Criminal Rambo=new Criminal();
Rambo.showIDcard("Rambo");
Criminal BigCat= Rambo; //Alias
BigCat.showIDcard("BigCat");
Rambo.doCrime();
System.out.println("Waiting of justice.... ");
Rambo=null;
BigCat=null;
System.gc();
Criminal.TheRock.showIDcard("TheRock");
Criminal.TheRock.doCrime();
System.out.println("Waiting of justice.... ");
Criminal.TheRock=null;
System.gc();
System.out.println("Criminal put to death without final wish ! ");
}
}
class Criminal {
public static Criminal TheRock;
public Criminal(){System.out.println("Criminal born ! ");}
public void showIDcard(String Alias){
System.out.println(Alias +" ID: "+this.hashCode());
}
public void doCrime(){
System.out.println("Criminal on rampage, Killed an innocent Object ");
}
public void finalize(){
System.out.println("Verdict: Death penalty ");
System.out.println("Criminal's last wish : resurrection !!! " );
TheRock=this;
System.out.println("Criminal given a new refrence: TheRock ");
throw new RuntimeException();
}
}
Out Put........
In the beginning there was ....
Criminal born !
Rambo ID: 15347198
BigCat ID: 15347198
Criminal on rampage, Killed an innocent Object
Waiting of justice....
Verdict: Death penalty
Criminal's last wish : resurrection !!!
Criminal given a new refrence: TheRock
TheRock ID: 15347198
Criminal on rampage, Killed an innocent Object
Waiting of justice....
Criminal put to death without final wish !
Moral of the Story.
- Garbage Collector (gc) clears unrefrenced objects from the heap.
- Every Object may define finilize(), executed just before the object is removed from the heap.
- gc is moody, it works on its own will, System.gc() method is just a request not a compulsion (1.4).
- It is not guaranteed that finilize() will be executed for an object, mostly it does, if it happens in the middle of a long execution.
- any uncaught Exception thrown in finilize() is ignored.
- if during finilize() of an unrefrenced object, it gets reffered to again (as above), then gc cannot not remove the object from heap.
- if the object escapes the finilize() once, then the second time the finilize() is not called, the object is remove directly.
- finilize() methods are not chained (they dont work like constructors), ie. sub class finilize() does not implicitly call super class finilize(), we have to explicitly call super.finilize();
your comment / votes are the source of encouragement, so please. - Rafay.
|
| Other 1 submission(s) by this author
| |
Report Bad Submission
|
Your Vote
|
| |
Other User Comments
|
1/5/2004 10:30:27 AM:
Very intresting! Thanks! (If this comment was disrespectful, please report it.)
| 1/7/2004 5:55:54 AM: VbNick
nice approach:) (If this comment was disrespectful, please report it.)
| 1/8/2004 12:39:30 AM:
The article is cool...A very good approach (If this comment was disrespectful, please report it.)
| 2/2/2004 10:48:21 PM: Priyaraj
Nice way of putting things. Good one (If this comment was disrespectful, please report it.)
|
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)
To post feedback, first please login.
|
|