This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a doubt on garbage collection.

 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers
I got a doubt in the question given below.
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.

To me i thinks it's option-4 but actual answer is given option-3 , is grabage collector behave like that way in those situations.
 
Pradeep Kadambar
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajit,

If you go through the Java Docs for finalize() you will find explanation for the answer being (c).


After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks pradeep, thanks a lot.
 
Anvi Dixit
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ajit

well i didnt got the question .

If the finalize() method of an object is re-references an object so that it becomes in-eligible for garbage collection

what does 're-refrences an object' means .

Thanking you in advance
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anvi Dixit:
Hi ajit

well i didnt got the question .

If the finalize() method of an object is re-references an object so that it becomes in-eligible for garbage collection

what does 're-refrences an object' means .

Thanking you in advance


For an object to make itself no long eligible for garbage collection, it needs to have a reference to it from something that is still reachable.

So it needs to have itself (ie. "this") assigned from some reference in some other object that is still reachable.

Henry
 
wise owen
Ranch Hand
Posts: 2023
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

See this,

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exact same information as the above listing is to be found in class Object's finalize method API documentation. So there is no need to go to the extent of getting the source code in this case - especially because it is more readable in the API.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic