This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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. ]
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
Joined: Jun 25, 2010
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?
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.
Joined: Jun 25, 2010
Thanks Matthew for your quick reply and sorry for the repeated questions.