• 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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, all!

When the program reaches line 8, how many String objects are eligible for garbage collection?
10 or 11?
I thought 10, but correct answer is 11! Why?

1: public class Jskdkdkkd {
2:
3:public static void main(String[] args) {
4:for (int i = 10; i >= 0; --i) {
5:String s = new String("String") + i;
6:System.out.println(s);
7:}
8:System.out.println("End");
9:}
10: }
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you will understand that the loop runs 11 times as from 10 to 0 its 11 u can count on that,here is ur program which i modified to show the loop runs 11 times.



Hope this will help u..
Thanks
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1: public class Jskdkdkkd {
2:
3: public static void main(String[] args) {
4: for (int i = 10; i >= 0; --i) {
5: String s = new String("String" + i);
6: System.out.println(s);
7: }
8: System.out.println("End");
9: }
10: }

yes 11 is right 10 to 0 is 11 see you are creating object inside for loop reference variable scope is inside for only so as soon as you come out of for loop you can not use s or refer s hence 10 to 0 means 11 objects eligible for gc.

however if we say

String s=null;
for(i=10;i>=0;i--)
{
s=new String("String " + i);
}

now 10 objects as final object s=String0 can be refered outside for loop mean it is not eligible for gc as its scope is not limited to for block


hope it helps.
[ September 13, 2005: Message edited by: Santana Iyer ]
 
Alexey Korneychuk
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excuse me for my mistake!
I thought correct answer is 11 becouse count of iterate is 11.
But correct answer is 10. Last String object do not eligible for garbage collection. Why?
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct answer is 10 and not 11 because the last String reference variable is refering to an object, so it is not elligible for garbage collection while the earlier objects were left abondened on the String pool. Since no reference variable was refering to it, so those objects became eligible for garbage collection.

Hope this will solve your doubt.

Enjoy Coding
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No friends,
The correct answer is still 11.
11 objects are garbage collected at line 8.
and to prove this..here is a little bit of modified code where in i have introduced my own class instead of String haveing finalize method.



and look whats the output:
---------------------------
String10
String9
String8
String7
String6
String5
String4
String3
String2
String1
String0
GC String0
GC String1
GC String2
GC String3
GC String4
GC String5
GC String6
GC String7
GC String8
GC String9
GC String10
End
-----------------------------
can you see how many times its written GC..

hope this point is clear to all the peopel here...if there can be any better explinations those are welcome..

Sandy
[ September 13, 2005: Message edited by: Sandeep Chhabra ]
 
Alexey Korneychuk
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With J2SDK1.4.2. the same.
But
I use WebSphere Studio Application Developer (Windows)
Version: 5.1.2

String10
String9
String8
String7
String6
String5
String4
String3
String2
String1
String0
End
GC String1
GC String10
GC String9
GC String8
GC String7
GC String6
GC String5
GC String4
GC String3
GC String2

"GC String0" I did't find in folowing list!
What is mind?
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i run the code repeatedly and always got the same result.
i dont understand what is the problem at your side.

also my result is

GC String0
GC String1
GC String2
GC String3
GC String4
GC String5
GC String6
GC String7
GC String8
GC String9
GC String10



where as yours is


GC String1
GC String10
GC String9
GC String8
GC String7
GC String6
GC String5
GC String4
GC String3
GC String2



may be someone else could help you out.

Sandy
 
anand phulwani
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This Arises A Question that when GC Runs,Whether it collects all the Objects eligible for garbage collection or few just enough to free the memory.
 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Sandeep,
I'm Agree With
"This Arises A Question that when GC Runs,Whether it collects all the Objects eligible for garbage collection or few just enough to free the memory."
Implementation also matters 'coz jls dosn't specifies which algorithm 2 use actually or run 2 collector even!This is Implementation Dependent


##################################################
Agrah Upadhyay
B.Tech 3rd Year
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Well i dont think that any question related to Garbage Collector's algorithm can appear in the exam. so this question seems superfluous.

but this would need comments of some experienced member of JavaRanch.

Sandy
 
Don't MAKE me come back there with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic