File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection & finalize() calls

 
Michael Powers
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Powers
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for explaining. Makes perfect sense now.

Michael
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic