I am confused little bit on gc and finalize() ..please correct me.. 1. We don't HAVE CONTROL on garbage collection. [yes/no] 2. It is upto JVM to garbage collect on "heap of objects". [y/n] 3. Box b = new box(2,3); b = new box( 3, 4); line 1 object is elgible for gc. [y/n] 4. Before gc , JVM is will EXCUTE finalize() method.[y/n] 5. If we don't code finalize(), will JVM execute the default finalize() method in Object class. 6. If we override finalize(), will JVM exceute ONLY overriden finalize() method. 7. what happens if there is an exception in finalize() method? 8. In JLS 12.6.1 , they use following terms a) finalizable b) finalized c) reachable d) finalizer-reachable e) finalized object f) unfinalized object can you guys explain above term? 9. Box b = new Box(3,4); b = null object in line 1 will be eligible for gc?
Sethuram, Tried to putdown comments to some of your questions. 1. We don't HAVE CONTROL on garbage collection. [yes/no] Comment: We have some control on GC. You can request JVM to do GC by using one of the System.gc() or Runtime.getRuntime.gc() methods. So the Answer is No. 2. It is upto JVM to garbage collect on "heap of objects". [y/n] Comment: Yes..It is upto the JVM to do GC. But if you think you have enough time (before user responds in your applet etc), you can make a request to JVM to carry on GC as explained above. 3. Box b = new box(2,3); b = new box( 3, 4); line 1 object is elgible for gc. [y/n] Comment: Yes. 4. Before gc , JVM is will EXCUTE finalize() method.[y/n] Comment: Yes. 5. If we don't code finalize(), will JVM execute the default finalize() method in Object class. Comment: No..JVM will try to execute the inherited finalize() in the super class, if any. 6. If we override finalize(), will JVM exceute ONLY overriden finalize() method. Comment: Yes..So whenever you override the finalize method, it is advised to invoke finalize() of the super class. 7. what happens if there is an exception in finalize() method? Comment: The exceptions raised in finalize() are ignored. Meaning the JVM will not try search the stack to find the exception handler. 9. Box b = new Box(3,4); b = null object in line 1 will be eligible for gc? Comment: Yes� Object in line 1 is eligible for GC. Please correct me if I am wrong. That leaves question 8 unanswered. Can some one answer this? Regards, Kondal.
Joined: Nov 22, 2008
Please read the following about finalization http://java.sun.com/docs/books/jls/class-finalization-rationale.html Does this mean that finalization has been removed from the JLS??? Or is this arguing that it should be? I know that the JDk and other compilers recognize the finalize methods, but are they just empty method bodies right now? Can one of our great byte code specialists check into this??
Java Nut- No. This only refers to finalization of classes - we still have finalization of objects. Finalization of a class is something that would have occurred prior to the unloading of an entire class, which only happens when there are no instances of a class anywhere and the JVM basically decides it no longer needs to remember what this class is and how it works. (Of course, it could decide to reload the class later.) This most commonly applies to anonymous classes - once the single instance of an anonymous class is collected, the JVM might as well forget the whole definition of the anonymous class as well. psethura- for question 8: a) finalizable Describes an object that the JVM has decided that it can (and eventually will) run the finalize() method on, but that hasn't happened yet. To be finalizable, an object must have first been unreachable. b) finalized The finalize method has been run on this object. It hasn't been freed from memory yet, but that will probably happen shortly if the JVM also verifies that it is unreachable. c) reachable Means that somewhere, somehow, it is possible for an active thread to find a reference in memory which leads to this object. If an object is reachable, it must not be garbage collected. Reachable does not include finalizer-reachable. d) finalizer-reachable Describes an object which can be reached only from objects which are currently listed as finalizable (which means that they themselves are not reachable from any active thread). The is a chance that a finalizer-reachable object could become reachable again as the result of some finalize() method. In this case, it will not be collected (i.e. deleted from memory) unless it later becomes completely unreachable. e) finalized object An object described by b) above. f) unfinalized object An object which is not yet finalizable nor finalized.
"I'm not back." - Bill Harding, Twister
Joined: Jan 30, 2000
psethura- I deleted your second post since I saw you also made it in Java in General (Beginner) and didn't want to have the same converation twice. Please go there for discussion of your additional questions. Re: question 8 and my post above: this level of detail is not necessary for the exam. It's nice to know about it, but if you're just after certification don't worry about it.