Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize help plzzz

 
ArnabKarmakar karmakar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a small code. When the program is over, the finalize function should be called. But it is not calling that one. You can have a look in my code.

According to me it should print "The object is going to be deleted" three times. But it is not printing.
Please help me out.
Thankz in advance.

Arnab

(You too can use [C0DE][/C0DE] tags )
[ October 09, 2004: Message edited by: Barry Gaunt ]
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not believe that the garbage collection will run automatically (and hence not your finalize method). If may not run even if you ask it to do so (kind of like trying to teach a cat to sit). I was able to get the output you are looking for with the following slightly modified code:

Note that although I have asked for the garbage collection to execute, it may not. At the very least, you have to provide some objects to be available for garbage collection before the application is finished which is what I have done by setting the object references to null.

Does this help?
 
ArnabKarmakar karmakar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you,
its working.
But can we always run the gc successfully whenevr we want.

arnab
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call System.gc() whenever you wish, but you cannot be sure that all eligible objects will be garbage collected, if any.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic