This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

finalize() Method

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I was just wandering, when will the code written in the finalize() method executed? I also tried using Runtime.getRuntime().gc() as well, but failed.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clarence J M Tauro wrote:

I was just wandering, when will the code written in the finalize() method executed? I also tried using Runtime.getRuntime().gc() as well, but failed.



You can call it yourself by saying:

e.finalize();

Other than that it runs when the garbage collector is executed, and the object is being gc'ed. And putting a call to the GC in your code doesn't actually run the GC. It is more like asking the JVM to run the GC, if it feels like it. It may run GC, it may not.
 
Clarence J M Tauro
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And putting a call to the GC in your code doesn't actually run the GC. It is more like asking the JVM to run the GC, if it feels like it. It may run GC, it may not.



Agreed..

But I have never seen the finalize() method getting executed..
e.finalize() is fine.. but I want to know the execution of finally method..
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clarence J M Tauro wrote:

And putting a call to the GC in your code doesn't actually run the GC. It is more like asking the JVM to run the GC, if it feels like it. It may run GC, it may not.



Agreed..

But I have never seen the finalize() method getting executed..
e.finalize() is fine.. but I want to know the execution of finally method..



In your code you have the potential for the finalize() method to be called. If the GC did run and collected your object the finalize() method would run.
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But I have never seen the finalize() method getting executed..
e.finalize() is fine.. but I want to know the execution of finally method..



Well, if you modify the program so that it doesn't exit -- ie. let it run a long time. And create and dereference many objects, you'll get it to gc. And hence, call your finalize() method.

Henry
 
Clarence J M Tauro
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This made it execute. Bad code though.

Thanks Henry
 
Clarence J M Tauro
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more related to this.

Now that I understand that the garbage collector might not necessarily execute before the termination of the program, what happens after the program terminates? The Employee object in the heap has to be destroyed right? (I mean garbage collected).
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clarence J M Tauro wrote:One more related to this.

Now that I understand that the garbage collector might not necessarily execute before the termination of the program, what happens after the program terminates? The Employee object in the heap has to be destroyed right? (I mean garbage collected).



I don't think garbage collected is the right term for it, but yes the object is destroyed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic