• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

another question regarding garbage collection

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
means object will be garbage collected or it's won't be allowed to be garbage collected.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic