• 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

Local variable and Garbage Collection

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain me the following sentence? I can't follow the explanation to the answer:
When the following method reaches line 8, how many of the String objects created in line 5 are eligible for garbage collection? Assume that the System.out object is not keeping a reference.
1.public void countDown()
2.{
3. for( int i = 10 ; i >= 0 ; i-- )
4.{
5.String tmp = Integer.toString( i );
6.System.out.println( tmp );
7. }
8.System.out.println("BOOM!");
9.}
ANSWER:
10
EXPLANATION:
Because only the last String object of the 11 created still has a reference. Because even though the tmp variable is out of scope in line 8, the local variable still has a reference.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't make sense. If "tmp" is out of scope, then it no longer exists and therefore no longer holds a reference.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Ron. All 11 objects are eligible for garbage collection when both the reference and the String objects go out of scope once control passes out of the loop.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniela Meyer,
There will be eleven String objects eligible for garbage collection after the method finishes execution.
I put some lines as shown below and ran the code ublic class Test
{
public static void main(String [] args)
{
Test obj = new Test();
obj.countDown();
}

public void countDown()
{
for( int i = 10 ; i >= 0 ; i-- )
{
String tmp = Integer.toString( i );
System.out.println( tmp );
}
System.out.println("BOOM!");
}
}
And I had the ff. result:
C:\java>java Test
10
9
8
7
6
5
4
3
2
1
0
BOOM!
Hence, if you check it, the for loop executed eleven times creating eleven objects on the whole.
Ikechukwu Morah.
 
Daniela Meyer
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to all of you!
11 was my answer, too, but the commercial MOC did tell me the answer described above.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's a "commercial MOC" and what is its URL?
 
Daniela Meyer
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron
A mock exam you pay for...
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though the last variable is out of scope at line 8, in some JVM it could not have cleared it by line 8, but it would wait untill the end of the method. Others however may have done it. That is why the exam is not going to test the eligibility of a variable for garbage collection in that context. I think Kathy Sierra has already posted something similar.
Joshua Bloch wrote on page 19 in Effective Java:


It should be noted that on present day JVM (JDK1.3) implementations, it is not sufficient merely to exit the block in which a variable is defined; one must exit the containing method in order for a reference to vanish.

 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, what Jose said
 
reply
    Bookmark Topic Watch Topic
  • New Topic