Author Topic:   garbage collection
sean zang
greenhorn
posted April 22, 2000 06:21 PM             
Dear all;
GC is still making me upset even though I read alot of materail
regarding GC. I can not verify the following statement with java source code. Does any SurperJavaer give me some jave code to verify the following statement. if not ,any detailed explaination will be greatly appreciated

Thanks in advance

Which of following statments are true
a: If object obj1 is accessible from object obj2 and object2 is accessible from obj1, then obj1 and obj2 are not eligible for garbage collection.

b: If an object obj1 can access an object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection.

c:Object will not be destroyed until they have no references to them

d: If an exception is thrown during excution of the finalize method of an object,then the exception is ignored and the object is destroyed.

These questions are from Khalid and Mughal's book

maha anna
bartender
posted April 23, 2000 11:11 PM             
This tests the concept of what happens if there are 2 objects and they both refer to each other. We have to be first clear , that when we say that an object is eligible for GC it means, there is NO MORE references holding this object anymore in any of the active threads in the program. For ex.


Vector obj1 = new Vector();
Vector obj2 = new Vector();
obj1.add(obj2);
obj2.add(obj1);

a: If object obj1 is accessible from object obj2 and object2 is accessible from obj1,
then obj1 and obj2 are not eligible for garbage collection.

True. Since both refer to each other these 2 objects are still referenced by other objects, and so not eligible for GC

b: If an object obj1 can access an object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection.

True. Since obj2 is referenced by obj1, YOU CAN'T MAKE OBJ1 ELIGIBLE FOR GC JUST BY SETTING ITS REFERENCE ALONE TO NULL. This is the key. Since this answer declares that obj2 is eligible for GC, which means the reference from obj1 is also dropped. which implies obj1 also WOULD HAVE BEEN set to null. Only then we can confidently say that obj2 has lost ALL its references and is eligible for GC.

This also says that obj1 is also eligible for GC. Why? obj1 is eligible for GC when obj1 = null and the reference from obj2 is also dropped. When this answer says that obj2 is eligible for GC, implies that obj2 is set to null ,which in turn says that the other refernce to obj1 from obj2 is also dropped.

The bottom line is, when there are 2 objects which refer to each other, in order to any one of the 2 objects to be GC'd the other one also must be GC'd.

So for the above code we have to set
obj2=null;
obj1=null;
for both of them to be GC'd.

c:Object will not be destroyed until they have no references to them

True. For obvious reasons stated above.

d: If an exception is thrown during excution of the finalize method of an object,then the exception is ignored and the object is destroyed.

Yes. Refer to JLS Here

From JLS
--------
20.1.11 protected void finalize() throws Throwable
If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

regds
maha anna

[This message has been edited by maha anna (edited April 23, 2000).]

Mani
ranch hand
posted April 24, 2000 01:14 AM             
I think B is not correct.

See this code

code:
 
public class strtest
{
public static void main(String args[])
{
String a=new String("test1");
String b=a;
a=null;
System.out.println(b);

}

}

Initially both refer to test1 object.
a=null; makes it eligible for GC.
b is still referencing test1.So I think statement B is wrong.


[This message has been edited by Mani (edited April 24, 2000).]

maha anna
bartender
posted April 24, 2000 07:35 AM             
No Mani. You code example is not correct example of the concept explained here. You have to give an example of Both objects obj1 and obj2 should refer to each other within them.

In your example 2 references to one object. THis is not correct example.Please read Maha's previous post in this thread carefully especially the 2nd answer.

And also your example is correct in its own concept.
regds
maha anna

[This message has been edited by maha anna (edited April 24, 2000).]

sean zang
greenhorn
posted April 24, 2000 02:28 PM             
Dear Maha
Thank you very much for your detailed explainnation.
Actually, I completely agree with you, But why the Author
give the different answer

a. invalid
b. valid
c. invalid
d. invalid

See Khalid and Mughal's book page255 8.1 and page 257 8.5

maha anna
bartender
posted April 24, 2000 02:33 PM             
For the first answer I presumed the 2 obj still have ref to each other ans they both are not yet set to null . So I said the answer is true. If the authors implies that they both are set to null, then 'false' is the answer.

What is the author's explanation. I don't have the book you mentioned.

regds
maha anna

Eric Barnhill
ranch hand
posted April 25, 2000 08:33 PM         
Maha Anna,

2 questions regarding your posts

1) regarding answer d, the passage you quote from the JLS is regarding an "uncaught exception" while the question just says "if an exception is thrown." probably the author meant uncaught exception, but isn't the answer false?

2) the terminology in the question "is accessible from" is unfamiliar to me from studying garbage collection. if a "is accessible from" b, does that mean that b contains a reference to a or is it vice versa? I have some more confusion there but I think if someone can just answer that, I'll be able to get the rest.

Thanks!
Eric B.

maha anna
bartender
posted April 25, 2000 09:28 PM             
Eric,
Thanks for pointing it out. When an exception is thrown during the finalization of an object , the exception is ignored and the finalization terminates which means the GC of the object is not complete and it is not destroyed.
So the answer d) is false

if a "is accessible from" b, means the object refereed by ref 'b' has a reference to the other object which is referenced by ref 'a'.

In simple terms what you asked for is correct. 'a' is accessible from 'b' means 'b' holds a the ref 'a'.

regds
maha anna

Eric Barnhill
ranch hand
posted April 25, 2000 09:50 PM         
quote:
----
if a "is accessible from" b, means the object refereed by ref 'b' has a reference to the other object which is referenced by ref 'a'.

In simple terms what you asked for is correct. 'a' is accessible from 'b' means 'b' holds the ref 'a'.
----
thanks for the extra clarification here. I wasn't properly distinguishing the objects from their refs in my head. Now i get it.

|