This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another question regarding garbage collection

 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Praveen Babu
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
means object will be garbage collected or it's won't be allowed to be garbage collected.
 
Rohit Suman
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic