Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC doubt!

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Assigning null to a reference causes the object to become eligible for garbage collection.


TRUE/FALSE ?



Help!

Thanks,
cmbhatt
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you possibly write a program to prove or disprove that statement?
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say it is false.

Objects become eligible for GC only when nothing points TO them, i.e. the references to them have been set to null.
But if an object in initialized, then put into an array and then the object is set to null, the array still uses it/references to it.

What do you think?
 
Omer Haderi
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is too abstract to answer, we must have take assumptions in order to answer.

case 1. if an object is referred by to references then it is FALSE
ex:


case 2. if an object is referred by one reference then it is TRUE


case 3. if a reference is directly assigned to null, then it's not TRUE neither FALSE. There is no object.


I hope in the exam this kind of questions contains code also, otherwise
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

1.Assigning null to a reference causes the object to become eligible for garbage collection.

Ans:This question as it is ambiguous. If you are forced to select between true and false then go for false.

2.Assigning null to a reference always causes the object to become eligible for garbage collection.

Ans: Definite False

3.Assigning null to a reference can cause the object to become eligible for garbage collection.

Ans: Definite True.

Thats my view point.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Omer and Srini for quick and helpful reply,

Srini, I completely agree with what three statements you jotted.
The first one is very ambiguous :roll: and definitely, saying it false will do the favor as I think too.

Next two, support you to decide quickly to answer.



I hope in the exam this kind of questions contains code also, otherwise

Tommaso, I hope this too!


Thanks,
cmbhatt
 
John Mathew
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
37. Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Mathew:
37. Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.


to a)
-> No, you can only suggest JVM to run the gc.

to b)
-> No, it is not guaranteed that the finalize() method is called. (K&B, SCJP 5, page 253)

to c)
-> I am not sure here, but I think this is correct, as finalize() is declared in the objet() class. Thinking twice it must be, because everything up the inheritance tree must be cleaned. It is vice versa of the constructor logic.

to d)
-> definitely not. You never know when it is going to run and what it going to do, besides cololecting garbage.

Good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic