File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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
Greenhorn

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="http://www.infoQuanta.com" target="_blank" rel="nofollow">infoQuanta</a> - parallel java programming made simple
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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 :
http://www.javaworld.com/jw-01-2001/jw-0112-singleton.html?page=5

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

thanks
Ramesh
Campbell Ritchie
Sheriff

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

thanks
Ramesh
"java -Xnoclassgc MyApp Command Line Argument" ??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection
 
Similar Threads
Ways to Improve My Web Application
When is it appropriate to use a singleton pattern?
Garbage Collection Problem
Singletons and memory leaks
Singleton Class and Garbage collection