• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

GC

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
1. public void countDown() {
2. for( int i = 10 ; i >= 0 ; i-- ){
3. String tmp = Integer.toString( i );
4. System.out.println( tmp );
5. }
6. System.out.println("BOOM!");
7. }

At line 6 , how may objects are eligible for garbage collection?
I chose 1 for tmp, but the answer says 10. Even if you include each of the Integer objects, the answer should be 11+1=12.
11 since the loop iterates 11 times.

------------------
Regards,
Shree
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before line 6 it should be 11 at 6 should be 12
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What about tmp. Can you elaborate?
------------------
Regards,
Shree
 
anjan bhushan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay I think only 11 will be eligible for GC.tmp wil hold one string
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shree,
The loop iterates 11 times and the 11th time String tmp holds Integer.toString(0). So there are 10 objects(Integer.toString(10).... Integer.to String(1)) which are eligible for GC.
 
anjan bhushan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about "BOOM!"
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The string tmp is declared within the for loop. At line 6 the for loop has ended and therefore, tmp is no longer in use. Doesn't that mean it is eligible for GC? Also, what happens when a method is quit -- is the memory for the local variables released automatically or they wait for the GC to free them?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with Ash,
as it is asked number of objects eligible for GC at line 6, so the answer would b 10, as even though temp is out of scope in line 6, the local variable still has reference.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Shree
here tmp is declared in for loop so when loop iterates 11 time then that eleven. so their should be 11 object eligible for garbage after for loops block over (at line 6)
Himanshu Amin

------------------
"JAVA BANAYE BAVA"
 
reply
    Bookmark Topic Watch Topic
  • New Topic