aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC 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 "GC" Watch "GC" New topic
Author

GC

Biby Thoms
Ranch Hand

Joined: Jan 10, 2006
Posts: 48
Can anybody tell how many object are eligible for gc after line 1 and how(Whizlab question). I thought the answer was 2. But the given answer is 3.

public class Test {
public static void main(String[] args) {
Test test = new Test();
String[] s = test.f()//line1
System.gc();
Thread.sleep(2000);
}
public String[] f() {
String[] s = new String[4];
for (int i=0;i<s.length;i++)
s[i]= new String(""+i);

String[] s1 = new String[2];
s1[0] =s[0];
s1[1]=s[1];
return s1;
}
}


Thanks<br />Biby Thoms
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178



After this function is over, objects s itself, s[2] & s[3] are all eligible for GC.
[ September 11, 2006: Message edited by: M.R.Krishnan ]
Patrick Patel
Greenhorn

Joined: Aug 25, 2006
Posts: 9
objects eligible for gc

s[2]
s[3]
test
Biby Thoms
Ranch Hand

Joined: Jan 10, 2006
Posts: 48
Thank you
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178

objects eligible for gc

s[2]
s[3]
test


How can 'test' be eligible for GC? It can be eligible only if it is explicitly set to null. Till the line 1, it is not null.
Rahuldf sharmadf
Greenhorn

Joined: Sep 18, 2006
Posts: 3
s[2], s[3] and string[] s
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC