How to unload a singleton

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Saruman

    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...)?
  • SPG

    #2
    Re: How to unload a singleton

    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 ReferenceMySing leton{
    public static void main(String args[] ){
    //Do not do this:
    MySingleton obj = MySingleton.get Instance();
    obj.getMessage( );

    //Do this instead
    MySingleton.get Instance().getM essage();
    }

    }



    "Saruman" <Anatoliy.Salis tra@gfinet.com> wrote in message
    news:dccaaaca.0 310201326.1ee49 662@posting.goo gle.com...[color=blue]
    > 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...)?[/color]


    Comment

    • Saruman

      #3
      Re: How to unload a singleton

      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 (uncaughtExcept ion 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...

      Comment

      • Saruman

        #4
        Re: How to unload a singleton

        "SPG" <steve.goodsell @nopoo.blueyond er.co.uk> wrote in message news:<Qc6lb.110 0$GQ2.10559529@ news-text.cableinet. net>...[color=blue]
        > 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;[/color]
        You mean 'static' here and in the method, don't you...
        [color=blue]
        >
        > public MySingleton getInstance(){
        > if( _instance == null){
        > _instance = new MySingleton();
        > }
        > return _instance;
        > }
        >
        > public String getMessage(){
        > return "Happy Days";
        > }
        > }
        >
        >
        > public class ReferenceMySing leton{
        > public static void main(String args[] ){
        > //Do not do this:
        > MySingleton obj = MySingleton.get Instance();
        > obj.getMessage( );
        >
        > //Do this instead
        > MySingleton.get Instance().getM essage();
        > }
        >
        > }
        >
        >
        >
        > "Saruman" <Anatoliy.Salis tra@gfinet.com> wrote in message
        > news:dccaaaca.0 310201326.1ee49 662@posting.goo gle.com...[color=green]
        > > 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...)?[/color][/color]

        Comment

        Working...