473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to unload a singleton

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
3 7214
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
3
by: Gauthier Segay | last post by:
Hello, I've an application where all my pages implement a PAGE_CODE string property, this property is stored in HttpContext.Current.Items. In some page, I must persist data in session while...
11
by: Timofmars | last post by:
I'm try to Unload DB2 data from a table into a record sequential file on NT. I can an unload on Mainframe, but it doesn't seem to be an option in NT. In NT, all I can do is export/import. I can...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.