• 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Garbage collection

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
R these two statements true regarding garbage collection :
1)You can directly run the garbage collector whenever you want to.
2)The garbage collector informs your object when it is about to be garbage collected.
Thanks in advance
Shallu
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) False. You can give the system a hint that it is a good time to do garbage collection by calling gc(), but that is no guarantee.
2) Sort of. Before an object is garbage collected the finalization method is run allowing system resources to be manually freed up before the object disappears. That could be considered informing the object. The exception is that it is possible to "resurrect" the object by creating a reference to it DURING finalization. Then at some future time when that reference has faded away and the object is ready for the garbage collector again, the finalization method is NOT re-executed. It is a one time only event that marks the object permanently as finalized. So in the rare event of a second life, it would not be informed again.
The JVM Specs on Finalization
[This message has been edited by Cindy Glass (edited April 26, 2001).]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 no, but you can suggest that it be ran.
2 The finalizer is called on your object just before the memory that it occupied is reclaimed.

In a nutshell there are 3 things you can do to facilitate garbage collection:
1. When you�re done with a reference to an object, set it to null. This way that object is marked for garbage collection.
2. Implement the finalize method for your complex objects. Here you could set all object references that this object used to null. The finalizer is the last thing that is called before the garbage collector reclaims the memory for the object. By the way, when one implements a finalize method, one is overriding the finalize method from the superclass Object.
3. You can suggest that the gc be ran with: System.gc() or Runtime.getRuntime.gc(). Java does not guarantee which object's finalizer will execute first. Garbage collection can be CPU intensive.
Hope this helps. Let me know if you have more questions.
Additional information:
Garbage collector is a low priority daemon thread (one that runs for the benefit of other threads.) The java language specification strangely, doesn't lay down the law on how garbage is to be collected. This way every implementer of a JVM is allowed to pursue the optimal method of gc.
 
reply
    Bookmark Topic Watch Topic
  • New Topic