Singleton resource in the JVM


Performance and scalability: Singleton resource in the JVM

  1. Singleton resource in the JVM (5 messages)


    I was wondering if anyone knows if there is a time limit in Weblogic 6 for holding on to a singleton class in the JVM. I had a situation where the singleton seems to expire in the JVM even when the application is still running. Could anyone confirm this? If this is the case, is the timeframe configurable?

    Thanks in advance for any help you may offer....

    Threaded Messages (5)

  2. Singleton resource in the JVM[ Go to top ]

    If the Singleton is first instantiated by a servlet, then it may expire when the servlet get's unloaded. Since a servlet that does not implement ImplementsSingleThreadModel can be unloaded at anytime time (as long as it's not fulfilling requests) it's hard to control this. In the past, I've used a single servlet that does implement ImplementsSingleThreadModel to load any singletons. Also, if I remember correctly, WL has startup classes. I don't know if these are in a seperate JVM instance or ClassLoader then what the servlets have access to, but it may be worth looking into have a startup class initialize the singleton and maintain a reference to it so that it doesn't get unloaded.

    Hope this helps more then hinders.

    - Dave
  3. Singleton resource in the JVM[ Go to top ]

    Thanks Dave,

    That does help. I'm using a regular java class to kick off the singleton instance, so I shouldn't run into a problem.
  4. Singleton resource in the JVM[ Go to top ]

    If no one is holding a reference to the singleton, there is no way to gurantee that the JVM will not garbage collect it.

  5. Singleton resource in the JVM[ Go to top ]

    Instantiating a singleton and maintain all such objects through an 'object preserver' thread does the trick for me. It can be in a startup/re-loadable servlet, or you can do lazy intialization and create/preserve the singleton at first use.
  6. Singleton resource in the JVM[ Go to top ]

    public final class Singleton {

      private static final Singelton instance = new Singleton();

      public final static Singleton getInstance()
         return instance;


    Assuming you use that kind of pattern you should be OK, since the class has a reference to itself. In _very_ old JVMs the class would get garbage collected, but this changed a long time ago, so that the class would not get garbage collected (at leaast not if it refers to anything.)

    I've used that pattern a lot and I've never had a problem.