• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC Doubt

 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please have a look at the following code:



Answer: a,b,c

Well, Should not it be b(Sceptical about b),c.

Please clarify my doubt.

Thanks
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also doubting a.Also b seems to be not in answer.

a is false as you can make a request to GCor not can make a explicit call to the Garbage Collector.
b is false as if suppose before a object is in the way for GCed an could not be collected due to some live reference then second time the finalize will not be called at all.
[ January 18, 2007: Message edited by: Sanjeev Singh ]
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I think it probably depends how you read the question.

The finalise is always called before an object is garbage collected - I'd say that is true. It's just not always immediately before (i.e called the first time it was eligible), but it is always before. I'd doubt in the exam they'd give you a question that ambigious.

And again for invoking the gc using a runtime object, yes you can invoke it, but it doesn't say that it will do anything.
 
Mandeep kaur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ONLY B IS RIGHT
---WHENEVER A OBJECT IS ABOUT TO BE GARBAGE COLLECTED,BEFORETHIS OPERATION FINALIZER IS ALWAYS RUN.'
--YOU CAN ONLY REQUEST gARBAGE COLECTOR,YOU CANT BE SURE IT WILL RUN OR NOT
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comments
1. a. is correct because you can explicitly call gc from a Runtime object
( I wrote code to so). Doesn't mean it will run.
2. b. is incorrect because finalize can be called on an object only once
(see K&B pg 253)
3. c and d are incorrect

So answer is a.

Where did you get this question from.
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See K&B ques 10 pg 263.

By "explicitly calling" the gc I understand as in the code below.
Am I mistaken ? The code compiles.

Runtime rt = Runtime.getRuntime();
rt.gc();
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is taken MindQ's Sun Certified Java Programmer Practice Test
Updated 4/7/99.

The answers that are given are a. b and c.

I disagree with b & c.

I would like one of our gurus to please answer this one.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic