• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection & finalize() calls

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for explaining. Makes perfect sense now.

Michael
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic