• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explain the ouput

 
B pandoo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test{
public static void main(Sting[] args){
Test test = new Test();
String[] s =test.f();// line 1
System.gc();
Thread.Sleep(3000);
}
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;
}

How many objects are eligible for garbage collection immediately after line1?
options:
0
1
2
3
4
5
6

I believe the answer is 4.Can anyone explain what might be the output.?
 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is 2.
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i also think that the answer is 4.
please expalain if i am wrong
 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Inside the method, you are creating 4 new String objects(s). Also you are creating 3 new String objects(s1). You are assigning objects denoted by s[0] and s[1] to s1[0] and s1[1]. So when the method returns, objects denoted by s[2] and s[3] are lost.

So the answer is 2.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your reasoning for saying the answer is 4?
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

Because of the String constant pool, the real exam won't ever use String objects in GC questions. The best thing to do here is to restate this question using objects of a type other than String.

hth,

Bert
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Restated with Objects. Answer I believe would still be two based off of same reasoning as Jothi proposed.

 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my reasoning for answer as 4:

apart from s[2] and s[3], there is one String constant "" which is used inside the method. This object is created in string pool as well as string heap when the loop executes first time so this object from heap should be garbage collected

And there is one object to which variable s refers (as there are total 5 objects for string array s, 4 are s[0],s[1],s[2],s[3] and one is variable s itself). So the object to which s refers should also be garbage collected

That is why i was supporting 4 as my answer.
Waiting for any explanation.

 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, even I guess 4 are eligible for gc. Alltogether we are creating 6 objects inside the method and we reassign 2 of them and we return those 2 which means 4 are left which can be gc.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rick,
The Foo f is not initialized.It should be like
 
B pandoo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class TestGC1 {
public static void main(String[] args) {
TestGC1 test = new TestGC1(); //line1
Foo[] foos = test.f(); //line 2
System.gc();
System.out.println(foos);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public Foo[] f() {
Foo[] f = new Foo[4];
Foo[] f2 = new Foo[2];
f2[0] = f[0];
f2[1] = f[1];
return f2;
}
}

class Foo {}

My explanation is: 1 test object on line 1.
then coming to the f() -->Foo[] of f reference itself is an object.
-->then Foo[](of f) points to 4 objects. namely
f[0],f[1],f[2],f[3];
-->Foo[] of f2 reference itself is an object.
--->then Foo[] (of f2) points to 2 objects
namely f2[0] ,f2[1].but this f2[0] points to f[0] and f2[1] points to f[1].fine.it now on the whole we
1)test object --> 1 object
2)Foo[] of f reference object -->1 object
3)--> its 4 objects -->4 object
4)Foo[] of f2 reference object -->1 object
5) --> its 2 objects refer to already existing objects of f[0],f[1] --> no object

on the whole we have 7 object.

once f() method returns, the FOO[] of f reference goes off.but its f[0],f[1] is still referred by f2[0],f2[1]. --->Inference the objects eligible for garbage collection is

1)Foo[](of f reference)
2)f[2],
3)f[3].

therefore out of 7 objects initially , 3 objects are garbage collected upto line 2.
correct me if i am wrong.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer should be 3.A total of 7 objects has been created on heap.These are
  • One TestGCed() object in line 3
  • Five object in line 14. One 1-D Foo[] array object and four Foo() object refered by Foo[] array references.
  • One 1-D Foo array object

  • The objects which are not being destroyed or nullfied
    Now TestGCed is not being nullified so will never be GCed.Once the method returns all the local variables will be nullified except those which are being retured by the method.Now the array reference f2 is pointing two Foo() objects and f2 is being returned by the method so threse three objects (one 1D array object refered by f1 and two foo object which this array is refere)will remain alive.So a total of 4 objects will not be GCed.
    The objects which can be GCEed
    It is one 1-D Foo array object referenced by f and two Foo objects which is being reference by the array itself are eligible for GCed.
    [ November 21, 2006: Message edited by: Sanjeev Kumar Singh ]
     
    Joe Harry
    Ranch Hand
    Posts: 10080
    3
    Eclipse IDE Mac PPC Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Guys, I guess we are talking on two different questions, one with the f array and the other with the s array. With the originally posted question by Pandoo, the answer is 4.
     
    Dan Polak
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Jothi Shankar Kumar Sankararaj:
    Guys, I guess we are talking on two different questions, one with the f array and the other with the s array. With the originally posted question by Pandoo, the answer is 4.


    NO The answer is 3

    there are created only 6 objects s, s[0],s[1],s[2],s[3], s1(empty!!!)

    next
    s1[0] -> s[0];
    s1[1] -> s[1];


    so egiligible object are:
    s, s[2],s[3]
     
    Vaibhav Chauhan
    Ranch Hand
    Posts: 115
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Daniel,

    apart from s,s[2],s[3] what i argue is what happens to the constatnt String object "". because as loop executes first tie this object is created in String pool as well as String heap so when the method ends, the one which is present in String heap is eligible for garbage collection.

    Therefore 4 objects should be eligible.
    Waiting for reply.




     
    raghava reddy yelamkonda
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi All

    We should consider array also an object.
    Inthat case first array object and its 2 elements(2 objects) in f() lost their refference after method returns. Other array object (which is stored in Foo[] array in main method) and its 2 elemnets(which are basically first array objects) in f() are their in main method.

    So answer is 3.

    Raghu
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic