By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,672 Members | 1,628 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,672 IT Pros & Developers. It's quick & easy.

How to unload a singleton

P: n/a
Unfortunately I am not able to find a direct answer to this question
neither in documentation nor in newsgroups. I have an application
biggest part of which is dynamically reloaded in certain circumstances
- kind of webstart, but dynamic (you have to restart webstart to get a
new version of an application) and without a GUI.

The problem is that I have some singletons among my objects. Turns out
that garbage collector has some trouble with affected classes. When I
load new versions of my classes, the instances of the old versions are
not finalized. Accordingly their classloader stays in memory, and all
old classes do too. The classes are therefore never unloaded. The only
workaround I've found is to explcitly to to every class and set the
singleton instance inside it to null. Then the famed Java unloading
kicks in. I understand that class unloading is optional for JVMs and
depends on implementation (mine is HotSpot), but still - is that how
it's supposed to work? Is Singleton pattern flawed in these
circumstances (not that I insist that my usage of it is always
justified...)?
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
SPG
My suggestion is never to directly access the object itself, always
reference it by the getter method, this way you do not hold references to
the signleton throughout your code, and garbage collection will happen as
you expect:

public class MySingleton{
private MySingleton _instance;

public MySingleton getInstance(){
if( _instance == null){
_instance = new MySingleton();
}
return _instance;
}

public String getMessage(){
return "Happy Days";
}
}
public class ReferenceMySingleton{
public static void main(String args[] ){
//Do not do this:
MySingleton obj = MySingleton.getInstance();
obj.getMessage();

//Do this instead
MySingleton.getInstance().getMessage();
}

}

"Saruman" <An***************@gfinet.com> wrote in message
news:dc**************************@posting.google.c om...
Unfortunately I am not able to find a direct answer to this question
neither in documentation nor in newsgroups. I have an application
biggest part of which is dynamically reloaded in certain circumstances
- kind of webstart, but dynamic (you have to restart webstart to get a
new version of an application) and without a GUI.

The problem is that I have some singletons among my objects. Turns out
that garbage collector has some trouble with affected classes. When I
load new versions of my classes, the instances of the old versions are
not finalized. Accordingly their classloader stays in memory, and all
old classes do too. The classes are therefore never unloaded. The only
workaround I've found is to explcitly to to every class and set the
singleton instance inside it to null. Then the famed Java unloading
kicks in. I understand that class unloading is optional for JVMs and
depends on implementation (mine is HotSpot), but still - is that how
it's supposed to work? Is Singleton pattern flawed in these
circumstances (not that I insist that my usage of it is always
justified...)?

Jul 17 '05 #2

P: n/a
Sorry for misleading, guys and gals. There is NO problem with
unloading classes with singletons, static object references etc. I've
had a problem with ThreadGroup though. We've overridden it to have our
own exception handling (uncaughtException method). Now, when you
create a new ThreadGroup, it's automatically added to the parent
thread group - 'main' in this case - and stays there until explicitly
removed. Understandably, the class cannot be unloaded - since an
instance of it is still active. Therefore the class loader and all
class definitions stay in memory. The problem is solved by calling
destroy() on an offending ThreadGroup (you have to make sure there are
no threads in it before that). Cheers...
Jul 17 '05 #3

P: n/a
"SPG" <st************@nopoo.blueyonder.co.uk> wrote in message news:<Qc*********************@news-text.cableinet.net>...
My suggestion is never to directly access the object itself, always
reference it by the getter method, this way you do not hold references to
the signleton throughout your code, and garbage collection will happen as
you expect:

public class MySingleton{
private MySingleton _instance; You mean 'static' here and in the method, don't you...

public MySingleton getInstance(){
if( _instance == null){
_instance = new MySingleton();
}
return _instance;
}

public String getMessage(){
return "Happy Days";
}
}
public class ReferenceMySingleton{
public static void main(String args[] ){
//Do not do this:
MySingleton obj = MySingleton.getInstance();
obj.getMessage();

//Do this instead
MySingleton.getInstance().getMessage();
}

}

"Saruman" <An***************@gfinet.com> wrote in message
news:dc**************************@posting.google.c om...
Unfortunately I am not able to find a direct answer to this question
neither in documentation nor in newsgroups. I have an application
biggest part of which is dynamically reloaded in certain circumstances
- kind of webstart, but dynamic (you have to restart webstart to get a
new version of an application) and without a GUI.

The problem is that I have some singletons among my objects. Turns out
that garbage collector has some trouble with affected classes. When I
load new versions of my classes, the instances of the old versions are
not finalized. Accordingly their classloader stays in memory, and all
old classes do too. The classes are therefore never unloaded. The only
workaround I've found is to explcitly to to every class and set the
singleton instance inside it to null. Then the famed Java unloading
kicks in. I understand that class unloading is optional for JVMs and
depends on implementation (mine is HotSpot), but still - is that how
it's supposed to work? Is Singleton pattern flawed in these
circumstances (not that I insist that my usage of it is always
justified...)?

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.