aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How Many Objects are eligible for garbage collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How Many Objects are eligible for garbage collection?" Watch "How Many Objects are eligible for garbage collection?" New topic
Author

How Many Objects are eligible for garbage collection?

Madhu Kumar Vatts
Ranch Hand

Joined: Apr 01, 2004
Posts: 74
Hi,

I was trying to solve questions related to Garbage Collection in Kathy Sierra SCJP book and I found that questions relating to the topic mentioned went wrong . I appreciate if some one could analyze and tell me how to answer these kind of questions. For sample, I am providing a questions given in the book..

public class X {

1 public static void main(String args[])
2 {
3 X x=new X();
4 X x2=m1(x);
5 X x4=new X();
6 x2=x4;
7
8 }
9
10 private static X m1(X mx) {
11 mx=new X();
12 return mx;

}
}
After line 6 how many objects are eligible for garbage collection..
A. 0
B. 1
C. 2
D. 3
E. 4

answer: B

class X2{
public X2 x;
public static void main(String args[])
{
X2 x2=new X2();
X2 x3=new X2();
x2.x=x3;
x3.x=x2;
x2=new X2();
x3=x2;//line 9
}
}

after line 9 , how many objects are eligible for garbage collection?

-Reference
SCP&D for java2 Study Guide Kathy Sierra and Bert Bates

Appreciate your time in replying

Thanks
Madhu

}


}


Oracle Certified Enterprise Architect (Part 1);SUN Certified Programmer;SUN Certified Business Component Developer;SUN Certified Web Component Developer;
Oracle Certified WebServices Developer;Oracle Certified Java Persistence API Developer;Oracle Certified SQL Expert;IBM Certified XML Solution Developer
Vidyavathi saravanan
Ranch Hand

Joined: Sep 24, 2004
Posts: 34
Hey Iam answering only the first question:

Here it is:

Line 3: Object X referenced as x created
Line 11: As a result of the function call at Line4 new X object created ands returned to main referenced as x2.
Line 5: New X object referred as x4 created.
Line 6 make x2 point to the object at x4. So two references are now pointing to the same object. And the second object which was previously pointed to by x2 is now abandoned. No taker for this. And hence this is the only candidate for GC now.

Hope this helps!!!

--Vidya
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
As to your second question, at first two objects are created, each referring to the other and each with a local reference variable in method main() pointing to it.
Then, a third object is created and by line 9 the two local reference variables both point to the third object.


At that point, the first two objects refer to each other but neither object can be accessed by code in any active thread.

So, the first two objects can be garbaged collected. BTW, GC algorithms are not fooled by the objects referring to each other - those objects are toast.


Mike Gershman
SCJP 1.4, SCWCD in process
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
I'm confused, I would interpret the 2 lost objects as a memory leak (not GC). Correct me if I'm wrong when an object is still being referenced it will not be garbaged collected. The 2 objects contain instances that refer to each other. Therefore by def'n they should still NOT be garbage collected.

Anyone else have any opinion?

Thanks


SCJP 1.4 <br />SCWCD 1.4 (preparing)<br />SCBCD 1.3 (coming soon)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
The garbage collector is smart enough to recognise groups of objects referring only to each other but with no way to reference them from outside that group.
If such a group is found by the garbage collector the entire group is garbage collected together, effectively removing one possible cause of memory leakage.

P.S. read up on "islands of isolation" which is what this is called.
[ October 29, 2004: Message edited by: Jeroen Wenting ]

42
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
How is a memory leak then possible if this islands of isolation are found by the gc.

Second in your assumption that these islands are gc, what if these islands are complex, it will take a lot of cpu cycles for the gc to locate these and clear them up.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
From the Java Language Specification:
A reachable object is any object that can be accessed in any potential continuing computation from any live thread
In the example, once x2 and x4 point to another object, the first two objects become unreachable and eligible for garbage collection. References that can never be accessed don't matter.
what if these islands are complex, it will take a lot of cpu cycles for the gc to locate these and clear them up.
Algorithms have been developed to make this process efficient. You could google garbage collection to see how.
[ October 29, 2004: Message edited by: Mike Gershman ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Many Objects are eligible for garbage collection?