Two Laptop Bag*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Will singleton class be vanished/unloaded, if not accessed/used by anyone? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Will singleton class be vanished/unloaded, if not accessed/used by anyone?" Watch "Will singleton class be vanished/unloaded, if not accessed/used by anyone?" New topic
Author

Will singleton class be vanished/unloaded, if not accessed/used by anyone?

Seetharaman Iyer
Ranch Hand

Joined: Jun 25, 2010
Posts: 35
Hi All,

My approach is Data class is singleton which will hold one instance of DBManager class (which performs low level I/O operations and memory caching - associated to one db file only)
Bear "reusability" in mind that I've created DBManager is public class. So my design will access Data class to perform all kind of I/O operations.

I would call Data class by Data.getData() - same approach of Runtime.getRuntime() class.
What's itching my mind is I'm not holding the reference of singleton Data object and Data class is holding its created instance by itself.
I've read somewhere in the net that, if VM reload the singleton class, everything will be (re)initialized (mean all static fields). Pretty straight forward one.
If there's no reference to the object then it'll be GC'ed. Here Data class holds the reference to it (no thread knows whether Data class has been unloaded and reloaded or not).

Let me come to my question: Very first time one client calls Data.getData() - Class is loaded and Singleton object is created and Class member is holding the reference to it. Then after this request, assume nearly a week/two, there's no request arrived to server to call again Data.getData(). In this case, will this class be unloaded by JVM? What will happen, after above said assumed long period when a new client make a request to the server to process some request?
Or simply tell me under what conditions, one class will be unloaded by JVM?

I'm not using any study guide or anything. So I couldn't confirm myself.

[I ran around 100 threads in different time like after 1 min, 10 mins, 1 hr to check whether JVM is using some time out to unload a class (and no thread is holding the reference variable to Data - use and throw mechanism just calling Data.getData()) and found same object is being referenced. ]

Please clarify me

Thanks in advance,
Seetha...

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

According to this article (which admittedly is pretty old), since JDK1.2 this hasn't been a problem unless the class is deliberately reloaded (which you won't be doing!). Specifically it says they use a
garbage collection model that forbids any class in a given classloader to be collected until all are unreferenced

Seetharaman Iyer
Ranch Hand

Joined: Jun 25, 2010
Posts: 35
Hi Matthew,

Thanks for your reply. This is the site where I've read about Singleton and the following words only makes me confused.

Matthew Brown wrote:
....until all are unreferenced



I'm not going to explicitly reload the class, but no where in my entire long-running program going to have the reference to either class or singleton object. As I said earlier, this is how I would use this class "Data.getData().read(recNo)" Should I assume that till the application exits?

Please help!

Seetha...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

As I understand that: the class will not be unloaded unless all classes in the classloader (which means all classes in the application in this case, if you're not doing anything unusual) are unreferenced. And as long as the application is still running that won't happen.

And since the class has a reference to the singleton, that means the singleton is safe from garbage collection as well.
Seetharaman Iyer
Ranch Hand

Joined: Jun 25, 2010
Posts: 35
Thanks Matthew for your quick reply and sorry for the repeated questions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Will singleton class be vanished/unloaded, if not accessed/used by anyone?