• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

SC ExamLab (Diagnostic). Question 70

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();

}
}
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

Have a look at the following post:
Q on GC from Exam Lab
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,
Got it.
Thanks for the clarification.

Regards
Edmen
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic