• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

GC doubt!

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


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



TRUE/FALSE ?



Help!

Thanks,
cmbhatt
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you possibly write a program to prove or disprove that statement?
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic