It's not a secret anymore!
The moose likes Java in General and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "garbage collection" Watch "garbage collection" New topic

garbage collection

Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
Hi Friends,
somewhere I could read this:
If no object holds a reference to the Singleton object, then the Singleton class may disappear, later to be reloaded when the Singleton is again needed. In that case, a new Singleton object will be created. Any static or instance fields saved for the object will be lost and reinitialized.

I just want to make sure that the VM in which I run my singleton class, should not call garbage collection, so that I can have its instance existing irrespective of its usage. Is it possible to set some command on my VM so that this VM should not call gc?

Thanks in advance !
Paul Fairhurst

Joined: Jun 12, 2008
Posts: 7
A Singleton requires a static field to hold its optional single reference, so that it can be given when the instance is requested again.
The existence of a static field holding a reference to it prevents the garbage collector from reclaiming it.

<a href="" target="_blank" rel="nofollow">infoQuanta</a> - parallel java programming made simple
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

This passage is very dated. Classes are actually never unloaded unless the ClassLoader that loaded them is itself garbage collected. Basically this means that any time there's a single instance of a single class in the JVM that could name the singleton class by name and get a reference to it, the class won't be unloaded. This really means that any time you could possibly, conceivably care, it will never happen.

[Jess in Action][AskingGoodQuestions]
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
Yes, you are right.
However, I could not understand the context of the following theory:

You can also set your VM to have no class garbage collection (-Xnoclassgc on the JRE 1.3, or -noclassgc on the IBM JVM.
it can be seen at this URL :

wanted to know, How can I set '-Xnoclassgc' ?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Originally posted by Ramesh Kumar Swarnkar:
wanted to know, How can I set '-Xnoclassgc' ?

"java -Xnoclassgc MyApp Command Line Argument" ??
I agree. Here's the link:
subject: garbage collection
It's not a secret anymore!