Prefer Static methods to Singleton

Discussions

General J2EE: Prefer Static methods to Singleton

  1. Prefer Static methods to Singleton (3 messages)

    As Singletons may have object level variables which may never get cleaned up and may cause memory leaks.
    Hence, I would suggest having all the methods static in the class.

    Input would be appreciated.

    -Jam

    Threaded Messages (3)

  2. Prefer Static methods to Singleton[ Go to top ]

    Static methods can access static fields that could still cause problems.
  3. Prefer Static methods to Singleton[ Go to top ]

    As Singletons may have object level variables which may never get cleaned up and may cause memory leaks.Hence, I would suggest having all the methods static in the class.Input would be appreciated.-Jam

    A static has exactly the same problem except that the variables are static now. How is that a solution? That is just a different problem :-), not a solution to the problem you have
  4. fix the problem[ Go to top ]

    The problem is not singletons. The problem is you need to define your strategy to release or cleanup resources. If you write a cache thats implementated as a singleton it can grow into a huge memory leak. Or you can choose to evict data from the cache every so often or cap the size. Either way there is only a memory leak if you allow it. Note that any java program will crash if you do not conserver your resources as memory is finite. Do not blame singleton. Just come up with a method for convserving and cleaning up your resources. Note that WeakHashMaps can be nice.

    Bruce