• 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

finalize() question

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question:
If the finalize() method of an object is re-references an object so that it becomes in-eligible for garbage collection

1) The compiler will throw an error.
2) The garbage collector "collects" the object anyway.
3) The garbage collector does not collect the object in the present sweep. But when the object becomes eligible for garbage collection again, its finalize method will not be called by the garbage collector (i.e., if the garbage collector gets a chance to run.). It will simply be garbage collected.
4) The object can never be garbage collected and hence leads to memory-leak. Each time the garbage collector calls finalize before "collecting" the object the object "resurrects" itself.

Answer is 3. Why? I thought it would be 4, since it is re-referencing the object whenever finalize()is called.
The API says that the finalize() is called only once, but what about cases like the above?
Also, can anyone explain the other kinds of common GC implementations, besides "mark and sweep"?
Thanks
Sharda
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sharda, just see if the following can throw light on ur issue.
First time when the object is de-refernced, it is eligible to be garbage collection. At that time, finalize method is called just before garbage collection.
In this case the object is re-refernced and so it is not garbage collected and again available in the program.
Next time again when the object is de-referenced, it is eligible to be garbage collected. But since finalize method will not be called the second time, so no chance of re-referncing. Therefore it will be garbage collected.
Also to know more abt Garbage collection like " stop-and copy) u can refer "Thinking in Java" by Bruce Eckel, Ch-4.
Regards
Shweta
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic