• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew for your quick reply and sorry for the repeated questions.
 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic