File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about garbage collection

 
kfu
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vasansrini, Thanks for the help.
Kevin
 
Doit
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got the order right. They are all referencing a, not d.
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic