• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I came across this question in Exam Prep by Bill Brogden. Practice test 1 question no.27.
____________________________________________________________
Here is a method which creates a number of String objects in the course of printing a series of messages.
1: public void soundOff() {
2: for (int i=1;i<10;i++) {
3: String tmp = Integer.toString(i);
4: System.out.print(tmp + ", ");
5: }
6: System.out.println("10");
7: }
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.
a. none
b. 1
c. 8
d. 9
____________________________________________________________
The given answer is c.8,
but I think d.9 is correct. Could some one tell me the correct answer?
Thanks,
Malar.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
variables that are local to methods are normally gced when the method returns. However, an optimizing compiler could gc them when leaving a specific block (in this case the for loop). BUT in the case of a normal compiler, at line 6 tmp (although not in the scope anymore) is still having a reference to the last created String instance, namely "9,". So at line six, again in the case of an non-optimizing compiler, there are only 8 String objects available for garbage collection ("1," to "8,")
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 05, 2001).]
 
Malar Ravi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valentin,
So if nothing is mentioned about the compiler does that mean we have to consider non-optimizing compiler for questions related to GC?
Thanks,
Malar.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Malar
Because we can't asumme anything about the compiler I think the correct answer is 9 because variables are made eligable for gc after the program has passed its scope. I hope in the exam the answer will be 9.
 
"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 variable is out of scope for you, but it lives on a stack that is associated with a "Frame" (not a GUI type frame ). The Frame is associated with a Method and holds the reference to the local variable until the method goes out of scope.
So the answer is 8.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation Cindy !

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cindy
 
reply
    Bookmark Topic Watch Topic
  • New Topic