Forums Register Login

SC ExamLab (Diagnostic). Question 70

+Pie Number of slices to send: Send
public class A{
public static void main(String[] args) {
test a = new test("obj1");
test b = new test("obj2");
test c = new test("obj3");
a.aob = b;
b.aob = a;
c.aob = a.aob;
test d=new test("obj4").aob = new test("obj5");
c=b;
c.aob = null;
System.gc();

}
}
+Pie Number of slices to send: Send
Hi all, please ignore the first post. I had accidentally keyed wrongly. Here i am,

public class A{
public static void main(String[] args) {
A a = new A();
A b = new A();
A c = A();
a.aob = b; //line 6
b.aob = a;//line 7
c.aob = a.aob; //line 8
A d=new A().aob = new A(); //line 9
c=b; //line 10
c.aob = null; //line 11
System.gc();
}
}

How many objects are eligible for GC after executing statement "c.aob=null"?

The answer is 2.

Here my drawing,

a -> A (obj1)
b -> A (obj2)
c -> A (obj3)

then
a -> A (obj1) -> A (obj2) //line 6
b -> A (obj2) -> A (obj1) //line 7
c -> A (obj3) -> A (obj2) //line 8

d -> A (obj4) -> A (obj5) //line 9

c -> A (obj2) //line 10, obj3 GCed here!!

c -> A (obj2) -> null //line 11

Where is another object??

Please help..

Regards,
Edmen
+Pie Number of slices to send: Send
HI,

Have a look at the following post:
Q on GC from Exam Lab
+Pie Number of slices to send: Send
Here is my sample of tracing,

public class test {

test aob;
String sName;

public test(String name){
this.sName = name;
}
public static void main(String[] args) {
test a = new test("obj1");
test b = new test("obj2");
test c = new test("obj3");
a.aob = b;
b.aob = a;
c.aob = a.aob;
test d=new test("obj4").aob = new test("obj5");
c=b;
c.aob = null;
System.gc();

}
public void finalize(){
System.out.println("finalize called for: "+sName);
}
}

The output is :
finalize called for: obj4
finalize called for: obj3

I am puzzling with obj4. Anyone know? Kindly please help.

Thanks

Regards,
Edmen
+Pie Number of slices to send: Send
Pretty good drawings... but you have amistake here....



Keep in mind that the assoc for assigments are right to left, so it is not this...



It is this...



One of the two objects instantiated at line 9 is no longer reachable by the next line.

Henry
+Pie Number of slices to send: Send
Hi Henry,
Got it.
Thanks for the clarification.

Regards
Edmen
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1002 times.
Similar Threads
doubt about GC
Confused about the output(Garbage collection)
Problem in Garbage collector
Is this legal syntax ? A d = new A().aob = new A();
Garbage Collection
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:28:56.