File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collector" Watch "Garbage Collector" New topic
Author

Garbage Collector

Kumar sridhar
Greenhorn

Joined: Apr 03, 2011
Posts: 6
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

Joined: Feb 24, 2006
Posts: 6662
    
    5

Save the "this" reference.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Deepak Bala wrote:Save the "this" reference.


Because if a reference points to itself, how can it be eligible for Garbage Collection?


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

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...


Java Experience
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Goodmorning Stephan... Thank you very much!...
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Aug 31, 2010
Posts: 394

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

Joined: Dec 06, 2010
Posts: 448

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

Joined: Oct 15, 2010
Posts: 294

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.


SQL Query Interview Question
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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!.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collector