• 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
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

interesting gc problem

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a method which creates a number of String objects in the course of printing a count down sequence.
public void countDown() {
for( int i = 10 ; i >= 0 ; i-- ){
String tmp = Integer.toString( i );
System.out.println( tmp );
}
System.out.println("BOOM!");
}
When the program reaches line 6, how many of the String objects created in line 3 are eligible for garbage collection? Assume that the System.out object is not keeping a reference.
OPT:a. none
OPT:b. 1
OPT:c. 10
OPT:d. 11
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think 11 different String objects lost their references and were ready for gc.
 
Gaurav Goyal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Cameron Park
could u plz provide an explanation fot it
I think it would be 10.
as there r 10 string object created.
then which one is the 11th
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to this has been discussed in detail at http://www.javaranch.com/ubb/Forum24/HTML/000038.html
gaurav the iteration happens 11 times so 11 objects will be created. How many would be gc'ed ? well that depends on JVM implementation. See Griscti's comment in above post.
regards
[This message has been edited by Anshul Manisha (edited July 06, 2001).]
 
Marshal
Posts: 17008
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gaurav, since your loop condition is i >= 0, i will be iterated through the following values:
10 9 8 7 6 5 4 3 2 1 0
That's 11 different values for i, 11 iterations, 11 times a new String object is created.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic