• 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

When will an object of a class will perish?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When will an object of a class will perish?
Can we explicitly do that by calling any inbuilt methods to perish the instance of a class ?
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set it to null, cleanly exit the methods, release all references and let the Garbage Collector deal with it.

You call tell the GC to fire via the API, but you are not guaranteed it will run "on demand".

WP
 
srikanth prasad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks William P O'Sullivan.
I can set the object references to null or I can request GC to get invoked either by System.gc() or Runtime.getRuntime().gc().
If I do either of these two, GC may or maynot fire the instances of the class from the heap.
But what I want to know is other than these, are there any ways to find when an object of a class will perish?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William P O'Sullivan wrote:Set it to null, cleanly exit the methods, release all references and let the Garbage Collector deal with it.

You call tell the GC to fire via the API, but you are not guaranteed it will run "on demand".

WP


How, exactly, do you set an object to null? you can set a reference to an object to null, but that won't guarantee anything.

and I believe you can suggest the GC run, not tell it to.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

srikanth prasad wrote:When will an object of a class will perish?



Define what you mean by "perish". That is not part of the standard Java lexicon.

If you mean "reclaim its memory," there's no way to force that in Java.

If you mean "make it so that its memory can be reclaimed, so that, as far as my program can tell, the object can no longer be used and its memory is, for all intents and purposes, available," then the answer is to ensure that there are no reachable references to it.

If you mean something else, please clarify.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic