File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection & finalize() calls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection & finalize() calls" Watch "Garbage Collection & finalize() calls" New topic
Author

Garbage Collection & finalize() calls

Michael Powers
Greenhorn

Joined: Feb 20, 2005
Posts: 6
Another question for you all - this time on which objects are eligible for garbage collection & the number of times finalize() is called:

I copied the following code from Bill Brogden's mock exam & ran it on my PC:

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
A reference to the third instance of TestQ38 is returned to main() and saved in obj. Only the first two instances of TestQ38 have no remaining reference when System.gc() is called.


Mike Gershman
SCJP 1.4, SCWCD in process
Michael Powers
Greenhorn

Joined: Feb 20, 2005
Posts: 6
Thank you for explaining. Makes perfect sense now.

Michael
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Is it sure that , output will be same for everytime . Because we are just requesting ( we can't force ) JVM to run GC . And finalize() method is called just before GC for that object . So in my opinion the ouput may differ ...

Please comment ... Thanks .
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I'd like to hear from someone with access to the Sun Java team, but I think that the gc in the jvm that comes with the j2se sdk is very aggressive in calling finalize() and destroying objects in order to help out students writing test programs like the one in this thread.

A production gc might wait until there is more to clean up.

Also, remember that the production use of calling System.gc() is not to prevent an out of memory condition - we are guaranteed that the jvm will take care of that - but to prevent any slowdown in processing during routine garbage collection from happening at an inconvenient time in a real time program.

The official specification about the unpredictability of gc does not forbid a particular implementation from being more predictable. Of course, the SCJP exam is based on the JLS and API doc and our answers should be as well.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
So the answer would be unpredictable ( If we have this option ) because in K&B & other cirtification book it is mentioned that we can't force GC by System.gc() this method .
Am I right ?
Thanks .
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Yes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection & finalize() calls