• 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

Garbage Collector

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Save the "this" reference.
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Saloon Keeper
Posts: 15529
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Goodmorning Stephan... Thank you very much!...
 
Stephan van Hulst
Saloon Keeper
Posts: 15529
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!.
 
reply
    Bookmark Topic Watch Topic
  • New Topic