General J2EE: Prefer Static methods to Singleton
As Singletons may have object level variables which may never get cleaned up and may cause memory leaks.
- Posted by: Ram Jam
- Posted on: October 18 2004 12:56 EDT
Hence, I would suggest having all the methods static in the class.
Input would be appreciated.
Static methods can access static fields that could still cause problems.
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
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.