File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about 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 "Question about garbage collection" Watch "Question about garbage collection" New topic
Author

Question about garbage collection

kfu
Greenhorn

Joined: Aug 08, 2000
Posts: 8
Two qestions about gb:
1)
/***********************************/
public class Test{
public static void main(String[] args) {
Object a = new Object();
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c=b=a;
d=null;

}
}
/***********************************/
How many objects will be eligible for garbage
collection after line d=null;?

2)
If object obj1 is accessible from object obj2
and object obj2 is accessible from object obj1,
then obj1 and obj2 are not eligible for garbage
collection.
I thought the statement was right and why is
it wrong? Why circular references do not prevent
objects from being gargage collected?
Thanks for help
Kevin


Kevin
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

1)
/***********************************/
public class Test{
public static void main(String[] args) {
Object a = new Object();
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c=b=a;
d=null;

}
}

d=c=b=a;
The above line makes all the four variables reference the same object - object a;
It means the other three objects which were originally referenced by the variables d,c & b become unreachable.
So they are subject to garbage collection.
"d=null" does not have any effect on the number of objects to be garbage collected because even though d is set null, the other three variables still have a reference to the object a;

/***********************************/
2)
If object obj1 is accessible from object obj2
and object obj2 is accessible from object obj1,
then obj1 and obj2 are not eligible for garbage
collection.
I thought the statement was right and why is
it wrong? Why circular references do not prevent
objects from being gargage collected?
The answer to the previous question should explain this.
kfu
Greenhorn

Joined: Aug 08, 2000
Posts: 8
Vasansrini, Thanks for the help.
Kevin
Doit
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
d=c=b=a;
means i think here all objects are refering to d .
as all assignements are done from right to left.
I am bit confused here. Please help.
- Thanks
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
You got the order right. They are all referencing a, not d.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
in the sentence d=c=b=a;;
all the 3 references [d,c,b] are pointing to the same Object to which reference a is pointing.soon after this line execution the 3 objects which were earlier referenced by b,c,d will be eligible for garbage collection cuz Norefrence is referencing to them and hence their referece counter dropped to ZERO.This is TRUE even after d= null ; though it is redundant and does not have any effect on garbage Collection.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about garbage collection
 
Similar Threads
Circular references and garbage collection
one question about gc?
Object which eligible to garbage collected
Jdiscuss question of the day - April 3, 2005
garbage collection