Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC

 
Biby Thoms
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
objects eligible for gc

s[2]
s[3]
test
 
Biby Thoms
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s[2], s[3] and string[] s
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic