aspose file tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Singletons and memory leaks Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "Singletons and memory leaks" Watch "Singletons and memory leaks" New topic
Author

Singletons and memory leaks

Gavin Bong
Ranch Hand

Joined: Apr 25, 2003
Posts: 56
Hi,
I am currently reading about the Singleton patterns and came upon some postings that say that Singletons are not thread-safe.
Firstly thread-safety in this context refers not so much to the creation of the singleton instance (i.e. use a load time, static initializer) but to mutable data in the singleton instance itself. However, I cannot see what's the problem with thread-safety if the methods that mutate instance member variable are synchronized ?
The author of the book Bitter Java, Bruce Tate said:
1) Re. the singleton, very useful. When you use them, be careful with your memory management. If you put items in a collection anchored by a singleton, it may never get garbage collected.
My question: why is this so ? If during the course of the program, the reference(s) to the collection goes out of scope; why isn't the keys and values in the collection garbage collected ?
2) Bruce goes on to say in the book that singletons have long life cycle ? Why is it different from any other Java class ?
Thanks
Gavin
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Gavin Bong:
1) Re. the singleton, very useful. When you use them, be careful with your memory management. If you put items in a collection anchored by a singleton, it may never get garbage collected.
My question: why is this so ? If during the course of the program, the reference(s) to the collection goes out of scope; why isn't the keys and values in the collection garbage collected ?
2) Bruce goes on to say in the book that singletons have long life cycle ? Why is it different from any other Java class ?

I believe Bruce is referring to the fact that usually, an instance of the singleton class is created close to program startup and will never be destroyed. In other words, once the singleton is instantiated, it will never be replaced with another instance. Thus the long lifecycle and that's why it's important to make sure that the stuff that singleton object holds references to are managed carefully.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Singletons and memory leaks
 
Similar Threads
class variable safe in struts 2 action ?
Boon's Mock Exam Q.65_Garbage
DAO (Data Access Object) as a Singleton - will objects created get garbage collected?
why use singleton to ensure thread safety
Singleton Class and Garbage collection