File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes another question regarding garbage collection 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 "another question regarding garbage collection" Watch "another question regarding garbage collection" New topic
Author

another question regarding garbage collection

Ajit Amitav Das
Ranch Hand

Joined: Dec 14, 2005
Posts: 49
Hi Ranchers one more doubt i have on garbage collection. and question goes like this.
Question :
If a Runtime Exception is thrown in the finalize method -

1.The running application crashes.
2.The exception is simply ignored and the object is garbage collected.
3.The exception is simply ignored, but the object is not garbage collected.
4.The Exception causes the JVM to crash.

What will be the answer wheather it will be option-3 or option-4 ?


Warm Regards<br />Ajit Amitav Das<br />SCJP 1.5
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
From the JLS,

If an uncaught exception is thrown during the finalization, the exception is ignored and finalization of that object terminates.
Ajit Amitav Das
Ranch Hand

Joined: Dec 14, 2005
Posts: 49
means object will be garbage collected or it's won't be allowed to be garbage collected.
Rohit Suman
Ranch Hand

Joined: Sep 09, 2006
Posts: 37
hi amit the exception will be ignored and the object will be garbage collected i.e, you can think in this way the finalize method terminates and if it is not garbaged collected this time next time when garbage collector runs it won't call the finalize method because the finalize method won't be invoked more than once.
Rohit Suman
Ranch Hand

Joined: Sep 09, 2006
Posts: 37
the answer to your question is number second for checking this you can create a finalize method and throw a runtime exception or any checked exception without handling it in try catch block see what's the output.
For example run this Program.
public class Test
{
private int[] bigArray=new int[50000];
public static void main(String args[])
{
Test a=new Test();
Test b=new Test();
Test c=new Test();
a=null;
b=null;
Test d=new Test();
Test f=new Test();
}
protected void finalize() throws Throwable
{
throw new Exception();
}
}
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Ajit Amitav Das:
means object will be garbage collected or it's won't be allowed to be garbage collected.


Can't be sure which is the case here because a new reference to the object could be created before the exception is thrown. If there is a new reference then the object would no longer be eligible for garbage collection.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another question regarding garbage collection
 
Similar Threads
finalize!
Exception in garbage collection
Garbage collection
a doubt on garbage collection.
Q on finalize() method