aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes one question about gc? 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 "one question about gc?" Watch "one question about gc?" New topic
Author

one question about gc?

Jason
Ranch Hand

Joined: Dec 06, 2000
Posts: 35
Hi,
Here is a question from Khalid and Rolf book (pp255):
I am reading the answer in the book, even that I just did not get it. can anybody help me?
8.1 Select all the true statements:
1. An object will be gc immediately after the last reference to the object is removed.
2. if an object 1 is accessible from an object 2 and object 2 is accesible from the object 1, then both are not eligible for gc.
3. Once an object has become eligible fro gc, it will remain in this statusuntil it's destroyed.
4. if an object obj1 can access obj2 that is eligible for gc, the obj1 is also eligible for gc.
Can anybody comment on this topic?
sgwbutcher
Ranch Hand

Joined: May 13, 2000
Posts: 56
Actually, the full question (in my printing) is:

Which of these statements is true?
Select all valid answers.
(a) Objects can explicitly be destroyed using the keyword delete.
(b) An object will be garbage collected immediately after the last reference to the object is removed.
(c) If object obj1 is accessible from object obj2 and obj2 is accessible from obj2, then obj1 and obj2 are not eligible for garbage collection.
(d) Once an object has become eligible for garbage collection, it will remain eligible until it is destroyed.
(e) If an object obj1 can access an object obj2 that is eligible for garbage collection, then obj1 is eligible for garbage collection.

Comments:
(a) false. There is no delete in Java. Garbage collection can never be forced, only suggested and it is through a different mechanism. (System.gc() or Runtime.getRuntime().gc()).
(b) false. There is no way to tell when an object will be garbage collected...the garbage collector Thread is a low priority Thread (maybe it should unionize?)
(c) false. The key is that the active program/thread/method has to not have a reference to an object. Within any object of even minimal complexity there are tons of active references to other objects within in it.
Circular references don't matter...if they did then outer/inner class pairs couldn't be garbage collected (because an inner class has a reference to its enclosing class and if instantiated an outer class has a reference to its inner class). Another example of a circular reference is a Frame and its spawned Dialog.
(d) false. The object's finalize method which is called just before it is garbage collected could resurrect it.
(e) true. This is the simple case of an object with an instance member that is an object. Like,
public class Obj1 {
Obj2 obj2;

Obj1() {
obj2 = new Obj2();
}
// other methods
}
Now if somewhere in Obj1's methods, obj2 where set to null then the object it referred to would be eligible for garbage collection and an object obj1 still referred to elsewhere in the active program wouldn't be eligible for garbage collection but the statement stipulates that obj1 can reach obj2 so this isn't the case.
Instead, this is merely the case of an object, obj1, that is eligible for garbarge collection which has member variables, like obj2, that are active object references looked at from the point of view of those objects referred to. They are eligible for garbage collection therefore it must follow that the object that refers to them must be eligible for garbage collection as well or the reference wouldn't be active ones.
Did I make this better or worse?
Best regards,
Steve Butcher
exceptionraised@aol.com
[This message has been edited by sgwbutcher (edited June 28, 2000).]
[This message has been edited by sgwbutcher (edited June 28, 2000).]


-------------------------<BR>Steve<BR>sgwbutcher@aol.com<BR>http://hometown.aol.com/sgwbutcher/
Jason
Ranch Hand

Joined: Dec 06, 2000
Posts: 35
Hi,Steve,
Thanks alot, i got your point.
a little bit tricky!!
 
Consider Paul's rocket mass heater.
 
subject: one question about gc?