aspose file tools*
The moose likes Beginning Java and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
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.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
mrudul joshi
Ranch Hand

Joined: Nov 12, 2003
Posts: 54
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: garbage collection