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 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.
a: If object obj1 is accessible from object obj2 and object2 is accessible from obj1, b: If an object obj1 can access an object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection. 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 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. From JLS regds [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: Initially both refer to test1 object. [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. [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 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
|
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!
|
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
|
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'.
|
| | |