• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Garbage collection??

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


frnds

here i feel the answer should be only b.
but its wrong.This is from MindQ mock exam.iam doubtful about 1..
since we can get the get the runtime object and can call Gc...But its not sure that Gc would be invoked ..it is unpredictable right ..

can anyone tell me the answer please??

thanks

sri
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel it's 'C'
Any class that overrides the finalize method logically should call the super class finalize method ()


The finalize method is always called before an object is garbage collected

No it's wrong the finalize method will be called only once. Suppose if you assign the object to be garbage collected to a variable in a finalize method . it will not be garbage collected, but the next time when the object becomes eligible for GC then finalize will not be executed because it got executed already for this object.
[ October 11, 2005: Message edited by: Srinivasa Raghavan ]
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but srini..

in the KhalidMughal book it was given that it is not mandatory to call the super class finalise method...(in the question should word seemed me to be mandatory) ...what u said is also correct...
iam unable to point out the exact answer ..
can anyone help me in getting out of this???


thanks

sri
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I would go for B.
It is true that finalize method is invoked just before object is garbage collected. (ofcourse its not called again if a new reference to the object is made in finalize mthod, but actually the method is already been called be before the object is GC'd)

C is wrong since it says
Any class that includes a finalize method should invoke its superclass' finalize method.

and I dont think it is mandatory
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A is true for me... Even if we can not be sure that garbage collection will run on elligible objects, we can say that Garbage Collector is invoked calling aRuntime.gc()
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Seb Mathe:
A is true for me... Even if we can not be sure that garbage collection will run on elligible objects, we can say that Garbage Collector is invoked calling aRuntime.gc()



Seb, I have read statements saying that calling System.gc() or Runtime.gc() does not gurentee that GC will get invoked, but they only suggest the Runtime Environment to invoke GC.

How could be one so sure for option a?
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's just a question of vocabulary and the question is abiguous...

Calling gc() "runs the garbage collector" (from javadoc). But we cannot be sure when objects eligible for garbage collection will be recycled by the JVM.

That's just my opinion, I'm not an expert...
[ October 11, 2005: Message edited by: Seb Mathe ]
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandeep I agree that it's not must to call the finalize() method in super class . But option 'B' says
The finalize method is always called before an object is garbage collected . Is it always called before a object getting GC'd , i feel it's not because of the reason mentioned above.

So whats the final answer for this ?
Whats given in that book ?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I think it's just a question of vocabulary and the question is abiguous...


But you cant see any ambigious questions in real exam.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garbage collection is a really hard topic to write mock questions for. I think there are a lot of really good mock exams you can find, but you should always be a little suspicious of their GC questions. As you have all said in this thread, the wording is very tricky, and if you're not careful the wording can be ambiguous.

On the real exam, the wording of GC questions is crafted very carefully so that you won't run into a question like the one being discussed.

hth,

Bert
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct answer according to me are: b and d.
Option A is incorrect as we cannot explicitly run the garbage collector, we can invoke it. Refer to the word explicitly.
Option 'B' is correct as finalize() method is always run before an object is garbage collected. Those object that resussicae themselves in finalize() have their finalize() method executed.
Option 'C' is incorrect as if we do not call the super.finalize() method, the new finalize() won't be executed.
Option 'D' is right as garbage collector behavious is unpredicatble, when an object is finally garbage collected is not known.

Other please comment.
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, my answers :

a) Yes, we can invoke it, but we can not be sure it will run immediatly...
b) True, but just one time (if finalize makes this obj available again, finalize will not be called next time the object will be eligible for GC)
c) They must not, but I think it's a good practice...
d) Not "very" for me, I just know some GC tuning features...

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic