aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

Alexey Korneychuk
Greenhorn

Joined: Sep 06, 2005
Posts: 18
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: }
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
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


Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
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

Joined: Sep 06, 2005
Posts: 18
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?
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
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
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
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 ]

Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Alexey Korneychuk
Greenhorn

Joined: Sep 06, 2005
Posts: 18
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

Joined: Aug 28, 2005
Posts: 340
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

Joined: Sep 10, 2005
Posts: 242
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.
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
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

Joined: Aug 28, 2005
Posts: 340
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Garbage collection