Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
Tosin Adedoyin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
also
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tosin Adedoyin:
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

0

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.

A, B, C
 
Tosin Adedoyin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi corey
thanks but i thought the ans willbe 1 cos at least name will be because it is set to null how do you know when they will eligble is there a hard and fast rule
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In most cases, you would be right. However, String literals behave differently when it comes to garbage collection. There is a reference created to them from a special table so they are never eligible for garbage collection. Therefore, the answer is 0. Take a look at this thread for a good explanation of how this works.
Corey
 
Tony kunds
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi corey,
I think the answer should be 1
as "Nick" has no references to it,it is eligible for GC.
I thought that i am confident about string gc.With your answer i am confused .
Please help.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that all of the Strings in this example are "String literals." There have been a number of threads going around here lately discussing the way String literals are handled. In short, thet aren't garbage collected because, even though you don't see it, there is still a reference to that object.
Check out this thread for a good discussion of the garbage collection of String literals.
I hope that helps,
Corey
 
Tony kunds
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi corey,
I gone through the link you gave,
the strings created like this are not Garbage collected
1)String="NICK";
but string created like this can be GC
2)String=new String("NICK");
Is it true.
I did many mock exam and the answers given are strings created like in 1 are garbage collected.
what should i select if I get a similar question.
I am giving the exam on sunday I am confused right now.
thanks.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony kunds:
Hi corey,
I gone through the link you gave,
the strings created like this are not Garbage collected
1)String="NICK";
but string created like this can be GC
2)String=new String("NICK");
Is it true.
I did many mock exam and the answers given are strings created like in 1 are garbage collected.
what should i select if I get a similar question.
I am giving the exam on sunday I am confused right now.
thanks.

Basically, that's true. The funny thing is that you've used a String literal to create the second String object, so you really have two different String objects - one of them in the literal pool (which you don't have a reference to, although you can get one) and one that you reference from the variable. The one in the literal pool would never be garbage collected while the new one would be eligible.
If you get a question like that on the exam, remember that literals are not garbage collected. That's the way Java works. If a mock exam says that they are, they're wrong.
Corey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic