Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
    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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Garbage Collection

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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 !
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Daneb, could u specify the mock test and question number and post this in the Mock Test Errata page?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
    Bookmark Topic Watch Topic
  • New Topic