Forums Register Login

MindQ q#36

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thank you all. That is clear.
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 841 times.
Similar Threads
gc ??
dout regarding garbage collection
Eligible for GC
MindQ #36
garbage collection
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:51:04.