Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramesh Kumar Swarnkar:
wanted to know, How can I set '-Xnoclassgc' ?

thanks
Ramesh
"java -Xnoclassgc MyApp Command Line Argument" ??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic