Should I use @Singleton for my impl, why?

4,414 views
Skip to first unread message

Andy

unread,
Apr 21, 2011, 6:20:38 PM4/21/11
to google-guice
I have a thread safe impl. What are the advantages and disadvantages
of using @Singleton.

I'm curios in terms of memory consumption by the Injector with several
singletons, and anything else that might be relevant.

Thanks!

Andy

unread,
Apr 21, 2011, 6:35:30 PM4/21/11
to google-guice
Do the same advantages and disadvantages apply to the “Singleton
Pattern”, as using @Singleton in Guice. (
http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons
)

Colin Decker

unread,
Apr 22, 2011, 12:44:05 AM4/22/11
to google...@googlegroups.com
No, a Guice @Singleton doesn't have the problems that the Singleton pattern has, since it doesn't involve a static instance or a lookup mechanism that causes tight coupling. It just involves Guice internally ensuring that it only creates 1 instance. The main things to consider when deciding whether or not to make something @Singleton in Guice are:

1) Does the class have state that needs to be shared between all clients of it and does it need to stick around for the lifetime of the application? If so, it needs to be a singleton.
2) If not, it depends on how you're using it. It should be faster to inject a non-singleton, so if it's going to be injected a lot (during web requests, say) don't bother making it a singleton. On the other hand, if it's being used in 10 other classes that are all singletons, it might as well be a singleton too to save a little memory.

-- 
Colin


)

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


Andy

unread,
Apr 22, 2011, 12:59:31 AM4/22/11
to google-guice
The service i'm using here just performs standard CRUD operations
against a database. I'm injecting it into several different actions
in Struts2

Why is it faster to inject a non singleton? I was anticipating a
singleton to perform better in terms in memory usage and injection
speed because of not needing to instantiate a new instance for each
injection.

Thanks for your answer!

Christian

unread,
Apr 22, 2011, 9:23:14 AM4/22/11
to google...@googlegroups.com
Hi Andy,

Object creation is extremely fast in modern JVMs and avoiding it shouldn't be used in order to speed up the program unless
you have a very specific case where you've shown it to be relevant.

+ in guice it allows you to bypass the scoping layer, look at http://code.google.com/p/google-guice/wiki/Scopes (Choosing a Scope, and also the comment #3).



2011/4/22 Andy <apry...@gmail.com>

--

Andy

unread,
Apr 22, 2011, 11:35:48 AM4/22/11
to google-guice
Thanks, I've read that page a few times, thanks for pointing out
comment #3. That pretty much answers my question.

To quote Crazy Bob.

"If the object truly is stateless, it's faster for Guice to create a
new instance than it is to retrieve a singleton. If Guice creates a
new instance every time, it can bypass the scoping layer entirely, not
to mention the logic inside the singleton scope."
Reply all
Reply to author
Forward
0 new messages