Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

eligibility for gc

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !
I've come accrose with a question which i don't understand
there is a code


The question is how many String obj created during the loop eligible for gc after its end.
I think the answer is 11 but the given answer is 10 .
Could you explain ? PLZ
Thanks a lot !
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer is infact 10 because the loop creates 11 objects but
only 10 are not reachable after the loop ends.The 11th object is
eligible for garbage collection after the method is finished.
str( 11th object ) is reachable until end of the method.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Kaspar on this one. The 11th String object is also eligible for GC after the for loop as there is no reference to it anywhere outside the loop. So considering that code I say the answer should be 11.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's 11 objects.. see the hashcode's below

public void m () {
for(int i=10; i>=0; i--) {
String str=Integer.toString(i);
System.out.println(str + " - " + str.hashCode());
}
System.out.println("End of loop");
}
output:
10 - 1567
9 - 57
8 - 56
7 - 55
6 - 54
5 - 53
4 - 52
3 - 51
2 - 50
1 - 49
0 - 48
End of loop
 
Elvis Rocha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The 11th object is
eligible for garbage collection after the method is finished


Yes, 10 objects are colected.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can this be little more brief please. I don't get the concept here.
In my view as soon as no reference is available for a object, it is eligible for garbage collectior. I could see that after the iteration, there is no reference available for the String Objects, so it has to be collected right.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q:
****************************************************************
The question is how many String obj created during the loop eligible for gc after its end.
I think the answer is 11 but the given answer is 10 .
Could you explain ? PLZ
****************************************************************
I think it is correct. Yeah, The loop creates 11 objects.
But, the first ten objects doesn't have the reference at all.
The 11th object has the reference but not used after the loop.
So, the first 10 objects are left orphan and eligible for gc.immediately.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kaspar Minosiantz:

The question is how many String obj created during the loop eligible for gc after its end.
I think the answer is 11 but the given answer is 10 .


I believe the answer should be 11. During each iteration of the loop, a new String object is created and assigned to the variable reference.
What many people seem to be missing is that the first object isn't eligible for garbage collection when the second object is created - it's actually eligible before that. When each iteration of the loop terminates and we check the condition for the loop, any local variables defined within the loop are now out of scope. Therefore, as the str variable is out of scope and that was the only reference to the String object that we had created in the previous iteration, it is now eligible for garbage collection.
This same principle holds for the first String object as well as the eleventh String object. Once each iteration of the loop terminates, the String object created within that iteration is eligible for collection.
Therefore, the answer should be 11, not 10.
I would be interested in seeing the explanation given for that answer, though, if one was provided.
 
Kaspar Minosiantz
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to everybody !
Corey McGlone

I would be interested in seeing the explanation given for that answer, though, if one was provided.


The explanation was that 11 th object still has a reference
as i've understood it's incorect , isn't it
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question on this one.
so, if the loop is i= 10, i<0, will the number oof objecct eligible for gc be 9 ???
Thanks
 
Vidya Sethuraman
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I rephrase the question.
so, if the loop is i= 10, i>0, will the number of objects eligible for gc be 9 ???
Thanks
 
Vinod Sinha
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes your answer is correct it would be nine in such a case
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kaspar Minosiantz:
as i've understood it's incorect , isn't it


Yes, that's incorrect. When the loop terminates, the reference variable that references it goes out of scope. Therefore, the object is eligible for garbage collection. If, however, the code looked like this:

Then you'd find that, even after the loop terminated, you would still have an active reference to the last String object created as the str variable would still be in scope.
 
Kaspar Minosiantz
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks !
 
If you're gonna buy things, buy this thing and I get a fat kickback:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic