Author Topic:   Garbage Collection
deneb shah
greenhorn
posted March 19, 2000 06:49 AM             
How many objects created in line 3 become eligible for garbage collection after the execution of the for loop ?

public void countdown(){
for(int i=10;i>=0;i--)
{
String tmp = Integer.toString(i);
System.out.println(tmp);
}
System.out.println("BOOM!");
}

Options given are
a) None
b) 1
c) 10 // Ans
d) 11

According to me the answer is 11

Could u please help me out !

Milind
ranch hand
posted March 19, 2000 09:05 AM             
Hi,
In my opiniom the answer should be None as the Strings won't be garbage collected unless they are created with new Keyword.
e.g String ex = new String ("example");
If in the given example the Strings are created with new keyword, 10 strings will be created and they will be eligible for gc after line 3.
Please let me know if I am correct.
Regds,
Milind

Jim Yingst
sheriff
posted March 19, 2000 11:16 AM             
If you go to the Search page (see link in upper right-hand corner of page) and type in "boom" you will find some past discussion of this question.

deneb shah
greenhorn
posted March 19, 2000 11:18 AM             
even strings in the string pol are garbage collected.

even though the new keywword would have been used dont u think that the number should have been 11 cuz the loop starts from 10 and goes till counter >= 0. so its 11 times ????

------------------
denice the menace

Jim Yingst
sheriff
posted March 19, 2000 01:57 PM             
Denice- all your points are addressed in this previous discussion (I see from the timestamp on your post that you were writing your post at pretty much the same time I was writing mine, so you probably hadn't seen my "Search" comment above.)

Summarizing: no, none of the Strings created in line 4 are on the String pool. "new" is not the only way to create a non-pooled string, it's just the most common one. The correct answer is indeed 11.

As for the intern pool (ignore the following if you just want to know certification-related stuff): it turns out that, contrary to some of my earlier posts on the subject, strings in the intern pool can be garbage collected in some JVM implementations. However, Strings created from string literals and compile-time constant expressions still can not be collected unless the class that makes use of a given string literal, is itself collected. GC of an entire class can only occur if there are no existing instances of the class anywhere, and none of the other classes in the JVM access constructors or static methods of the class which is about to be GC'd. Which is rarely the case.

The reason an interned String created from a literal cannot be collected as long as the class which created it is still in memory, is that the class keeps its own table of compile-time constants which contains a reference to the interned String. This table of constants is separate from the intern pool. As long as the class is in memory, the reference it has for the pooled String will prevent that String from being collected.

Followup questions about GC of string literals and the like should go to the Performance forum. It's not an issue that comes up on the certification exam.

Rolf Weasel
ranch hand
posted March 19, 2000 02:47 PM             
Daneb, could u specify the mock test and question number and post this in the Mock Test Errata page?

Jim Yingst
sheriff
posted March 19, 2000 04:15 PM             
Good point - I'll put a copy of the old discussion there with the correct subject line, since that's the discussion that has the better explanation IMO.

deneb shah
greenhorn
posted March 20, 2000 06:59 AM             
Hi Jim
Is there any way that we could structure the questions..
like could we group it exam wise or topic wise..
i feel that there is potential information stored in the ranch and if it is structured then believe me it is a reservoir for people who not only wanna learn java but also for people who are pro's in java..
( i m talking about structuring in the certification section )

Thank you for ur reply. i got my answer.

deneb

maha anna
bartender
posted March 20, 2000 07:33 AM             
deneb,
I also thought that. Are you thinking about structuring this 'Programmer Certification' forum according to the objectives of the Exam? If so, it has got one disadvantage. Now, all the messages are in the same place, it is sure to catch up all people who just comes into this forum, and has got more chances to be replied/discussed by all interested people. But if we put one more subDivision and , eventhough the 'color of the bulb' on the left changed, it still needs the user to click one more click for each and every sub-catagory right? ( ). I think this is better in that aspect. This draws attention of more people eventhough your suggestion is better for people coming here for searching for particular point/answer.

I am closing this topic, since I think your original qstn is answered already, and now we are going somewhere else. . If you want you can post a new topic at the 'Java Ranch' forum which is related to suggestions to improve this site.
regds
maha anna

|