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

MindQ q#36

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
36. 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
a) 0
b) 1
c) 2
d) 3
e) 4
the answer is b. Would somebody explain why because I think it is a. I thought there are 3 objects in the String pool here. Since they all have references pointed to them, none will be garbage collected. Thanks
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects in the String pool are never gc'd. So only 1 will be eligible for gc. There's lot of discussions on this in JavaRanch if you search for string pool.
My understanding is that this type of question will not appear in the exam, according to this post.

------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Quan,
As I see the problem, James comment about String pool doesn't apply to this problem. Lets step through the code to see how come b is the right answer.

Line 1 gives us no new objects. Line 2 gives us the first object. Our memory now looks like this:
"Nick" <-- newName
Line 3 redirects the newName reference to point to another object. Our memory now looks like this:
"Nick"
"Jason" <-- newName
Line 4 finally gives name an object to reference. Our memory now looks like this:
"Nick"
"Jason" <-- newName
"Frieda" <-- name
Line 5 creates another reference variable and points it to the object already pointed to by name. Our memory now looks like this:
"Nick"
"Jason" <-- newName
"Frieda" <-- name <-- newestName
Line 6 removes the name reference by setting it to null. Our memory now looks like this:
"Nick"
"Jason" <-- newName
"Frieda" <-- newestName
Now we are at the line marked '// Line A'. Objects can only be garbage collected if no references to them exist. How many items in the above list have no references to them?
Learning one step at a time,
Manfred.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The issue of whether String Literals can be GC'd is being discussed here. http://www.javaranch.com/ubb/Forum24/HTML/008746.html
I believe that the current answer is that it is left up to the vendor to decide if they want to do it. If they DO do it, it is not until the class is unloaded which would presumably be past line A.
Based on that, the conclusion is that this is a poor choice for a question on GC.
 
quan zhu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. That is clear.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic