Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collector

 
Kumar sridhar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to solve Question no. 7.h from SCJP Practice Exams by Bert Bates in Chapter 4 titled "Coding Exercises".

The question is :

"Create a class that has a method such that the first time the garbage collector attempts to collect a given instance, this method will keep the garbage collector from collecting that instance at that point.".

Could someone please tell me how to go about it?

Thanks a lot,
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Save the "this" reference.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Bala wrote:Save the "this" reference.


Because if a reference points to itself, how can it be eligible for Garbage Collection?
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Coleagues, "Save the this reference"?? can any of you be kind enough to describe that with a few lines of code??.

I will sincerely appreciate. Thanks.

 
Stephan van Hulst
Bartender
Posts: 6323
78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:


I had read that once finalize has been invoked then the object is guaranteed to be garbage collected. Seems like the above code will not achieve the desired result. Please verify...
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Goodmorning Stephan... Thank you very much!...
 
Stephan van Hulst
Bartender
Posts: 6323
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunny Bhandari wrote:I had read that once finalize has been invoked then the object is guaranteed to be garbage collected. Seems like the above code will not achieve the desired result. Please verify...


Have you tried running the code? What are your conclusions?
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
Sunny Bhandari wrote:I had read that once finalize has been invoked then the object is guaranteed to be garbage collected. Seems like the above code will not achieve the desired result. Please verify...


Have you tried running the code? What are your conclusions?


Ikpefua wrote:

Sunny Bhandari, I read something COMPLETELY different from what you read and my reference is Chapter 3, Page 263 of the K & B Book, please READ CAREFULLY:
______________________________________________________________________________________
There are a couple of concepts about the finalize() that you need to remember.

-For any given object, finalize() will be called ONLY once (at most) by the garbage collector

-Calling finalize() can actually result in SAVING an object from deletion
______________________________________________________________________________________

I actually ran the code that Stephan posted here and it gave me the following result:

Garbage collector is trying to collect me

Garbage collector failed

This perfectly comfirms the K & B book
______________________________________________________________________________________

I will appreciate it if you can quote your source, this will help us a lot, another 'possible occurence' is that you
misread your source's explanation, no one will blame you for misreading since that usually happens to everybody.

Thank you for your understanding.
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I don't remember the book name and author but it was an SCJP preparation book.

The logic being given was that finalize() is called only once and hence we should not store a reference to this in the finalize() method and I confirmed that by invoking the system.gc again in the code as:



which proves that indeed finalize is called once.

The solution above is correct because according to problem, we are not concerned about what happens the second time GC tries to collect that same instance.
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , finalize() method is called only once by Garbage collector thread but can be called multiple times by any other thread just like normal method.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javin Paul wrote:Yes , finalize() method is called only once by Garbage collector thread but can be called multiple times by any other thread just like normal method.


Hello Javin, At first I thought you were wrong, but when I read again expecially the end of your sentence 'just like normal method', I agree with you its VERY true!.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic