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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Hi
I have a few questions on garbage collection.I am totally confused.
1) When is a finalize() method of an object invoked(if finalize() overridden) ? Does ir happen just before garbage collection??
2)When System.gc() is invoked I understand that garbage collection is not guaranteed. But are finalize() methods automatically invoked when this call to System.gc() is made?
3)When System.runFinalization() is called are finalize() methods always invoked?
4)Are finalize() methods only overridden when there is a need to call native methods which release some memory resources.
Thanks
Marilyn
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
1 - This is one of those areas that the language specification outlines what should happen, but leaves the actual implementation up to the JVM. What should happen, is: some time between when an object becomes available for garbage collection and when the memory is actually freed, the finalize method will be invoked. However, since it is up to the actual implementation of the JVM, you have no guarantee when it will actually run, or in what order possible candidates will chosen. You can request this happen with a call to System.runFinalization, which suggests that the JVM invoke the finalize method of all objects that are ready for garbage collection, but once again, it's just a suggestion. It's up to the JVM whether or not to actually do it.
2- No, see above.
3- No, see above.
4- You can use finalize for any non-memory resource ie. network handles, file handles, etc.
Scott Palmer
Ranch Hand

Joined: Jul 11, 2001
Posts: 31
Can you even be 100% sure that the finalize method will ever run?
I thought I read somewhere that if the app exits and the JVM shuts down it doesn't have to bother with all the objects that can be collected since the whole java heap is about to be thrown away. That means that even if the garbage collector had marked some objects as collectable so they are on a list of object for which the finalize method should be invoked, there still may be some objects that the collector hasn't found yet and as i understand it there is no requirement that it clean up everything neatly if the JVM is being shut down.
Normally if the JVM shuts down the process is ending and the resources it had a hold on are released to the undrelying OS anyway.. but if you are trying to use finalize methods in other ways e.g. like decrementing a reference count to some bit of native code stuff that may still be running when the VM exits you could be in trouble..
Could someone with more experience confirm this or set me straight?
Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Hi
Then can I predict how garbage collector works and when finalize() mehtods are invoked if i know how a particular JVM is implemented.
Marilyn
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
I agree with Scott. I don't think the finalize method is an appropriate place to perform coding such as incrementing a variable. From what I understand the intention of the finalize method is, basically, the programmers last chance to clean up any resources that may still be open.
It is not intended to be a crutch for poor programming, either. A well written program will still attempt to free resources during "normal" progression of the program, ie. before the end of the main() method (be it via a call to System.exit or running the final line of code, or whatever). The finalize method is a last ditch safety net. And if the statements in it are of the type:
variablex = somevalue;
or
someResource.close();
then something is wrong.
Appropriate statements for the finalize method should be of the nature.
if( !( someResource == null ) )
{
someResource.close();
}
The primary cleanup for someResource should be in a more appropriate area.
It is theoretically possible to examine a particular JVM and increase the predictability of the gc and runFinalization methods (actually I think the major JVM's respond to runFinalization calls immediately), but why? The point is Java is Java. It is intended to run within a certain environment. If your code is dependent upon the exact moment either the gc or runFinalization method executes then perhaps your code needs to be altered in a manner that removes that particular requirement; or Java isn't the most suitable language for that particular application; or you are working in a very specialized environment, requiring very specialized coding practices and/or Java implementations that are well beyond the beginner forum.
[This message has been edited by Guy Reynolds (edited July 18, 2001).]
[This message has been edited by Guy Reynolds (edited July 18, 2001).]
Scott Palmer
Ranch Hand

Joined: Jul 11, 2001
Posts: 31
Marilyn,
Why would you want to write code that only works for a single JVM? Not only do you lose portability, but what if a bug fix is released for that VM and it breaks your code?
Maybe if you are shipping your application with a specific VM you can get away with this in the short term.. but it sounds like it will be a maintenance nightmare should you need to upgrade the VM.
Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Hi Scott and Guy,

Thanks for your answers and suggestions.
Will put forward some more questions when confused.
:-)
Marilyn
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370


Really Nice disussion on Garbage collection!

------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
 
Don't get me started about those stupid light bulbs.
 
subject: Garbage Collection