• Post Reply Bookmark Topic Watch Topic
  • New Topic

finalize() not called...Garbage collection question  RSS feed

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is there any reason,why finalize() of Hello class is not being called? not even at the termination of the program?!!?!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM never needs to reuse the memory, so the Hello object is never actually collected. The JVM does not collect all objects on exit, either.

You can stick a few calls to System.gc() in there, which may force the Hello object to be collected.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:The JVM never needs to reuse the memory, so the Hello object is never actually collected. The JVM does not collect all objects on exit, either.

Then what about those things the book says - 'The JVM will collect objects whose references are no longer needed'?!
Even on terminating,if JVM doesn't collect objects then...at some point of time,u'll have no free space at all,right?! I dunno whether i'm making sense myself... Just confused..
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:
Then what about those things the book says - 'The JVM will collect objects whose references are no longer needed'?!


If you have a book that says precisely that, it's wrong. It would be correct to say "The JVM may collect unreferenced objects when the memory they consume is needed for reuse."

Even on terminating,if JVM doesn't collect objects then...at some point of time,u'll have no free space at all,right?! I dunno whether i'm making sense myself... Just confused..


When a process like the JVM exits, the operating system (Windows, Linux, etc) will reuse all of its memory. There's no need for the JVM to collect the individual objects -- the whole process gets "collected" by the operating system.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!