Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
mrudul joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java has automatic garbage collector.we can define finalize()method that will be run by the garbage collector.if we explicitely define system.gc()in a program then can the same program have finalize() method?if yes, then how will the code written in the finalize()be executed?by whom?and how to transfer the program flow to the finalize()method?
if the program can contain the finalize()method and also System.gc()then what happens if System.gc()gets executed beforew the finalize()method?
thank you.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method System.gc() tells the JVM that running the garbage collector now might be a good idea. It's a suggestion, not a command, and the JVM can ignore it.
Before an object is garbage collected, the collector will call finalize() on that one object, yes. There's no guarantee, though, that any given object will ever be collected, you realize.
You can't redefine the System.gc() method, so most of your question doesn't make any sense.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To take Ernest's discussion a little further, because the GC runs unpredictably using the finalize() method is not generally recommended. It is there, and IF the GC runs, it will call an object's finalize() method before destroying the object, but it's too erratic to rely on for anything important.
 
mrudul joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for clearing my concept!i am preparing for java programmer certificcation and a lot of such small things that require basics haunt my mind . I am sorry to ask such a basic question.
thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic