This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

a doubt on garbage collection.

 
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.
 
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.
 
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
 
author
Posts: 23853
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Ranch Hand
Posts: 2023
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

See this,

 
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.
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!