When is the google guice injector garbage collected?

229 views
Skip to first unread message

vishesh gupta

unread,
Nov 17, 2017, 12:54:59 AM11/17/17
to google-guice

When we are no more referencing a google guice injector in the application, when will be the resources held by the injector, like Singletons, released or garbage collected?

To illustrated more, consider this code snippet:

public void someFunction() {
Injector injector = Guice.createInjector(new DataStoreBindings());
DataTransactor transactor = 
injector.getInstance(DataInfoTransactor.class);
transactor.doSomething();    
}

Some singleton bindings are also created by this injector. When this function ends the Injector instance will go out of scope and application has no way to refer this Injector again.

I want to know, if the injector will be garbage collected? If yes, then what will happen to the singletons held by the injector?

Stephan Classen

unread,
Nov 17, 2017, 4:26:41 AM11/17/17
to google...@googlegroups.com
They will also get garbage collected unless they have been injected somewhere. Then they only get garbage collected once the objects holding these references are also garbage collectable.
In short. Guice does not change any behavior of the GC. The injector and all created instances (singleton or not) will get collected when they are not reachable. Just like any other java object.
And because this question may arise as a follow up:
Singletons are not global singletons. They are singletons per injector instance. So guice will not hold a reference in some static field.
>--
>You received this message because you are subscribed to the Google
>Groups "google-guice" group.
>To unsubscribe from this group and stop receiving emails from it, send
>an email to google-guice...@googlegroups.com.
>To post to this group, send email to google...@googlegroups.com.
>Visit this group at https://groups.google.com/group/google-guice.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/google-guice/b2ec2692-0833-45b4-a85d-23adb6900ae4%40googlegroups.com.
>For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages