square / dagger

A fast dependency injector for Android and Java.
https://square.github.io/dagger/
Apache License 2.0
7.31k stars 3.07k forks source link

Holding reference after onDestroy() #519

Closed jemshit closed 8 years ago

jemshit commented 8 years ago

Question: does Dagger remove reference to injected variables on onDestroy() of Activity ? Here a guy wrote Activity holds reference to injected Presentervariable after Activity is destroyed. Someone enlighten me, thanks.

JakeWharton commented 8 years ago

Dagger knows nothing about Android and thus nothing about the Android activity lifecycle. Injected types are not automatically freed.

However, you interpreted that issue incorrectly. It is the presenter which is keeping a reference to the activity and preventing the activity from being garbage collected, not the other way around.

jemshit commented 8 years ago

So we make each injectedVariable=null on onDestroy() or any other quick solution to prevent memory leak? I thought @ActivityScope etc.. on Components destroy all injected variables after that scope (Activity) is destoyed? On Fri, Feb 19, 2016, 16:59 Jake Wharton notifications@github.com wrote:

Dagger knows nothing about Android and thus nothing about the Android activity lifecycle. Injected types are not automatically freed.

However, you interpreted that issue incorrectly. It is the presenter which is keeping a reference to the activity and preventing the activity from being garbage collected, not the other way around.

— Reply to this email directly or view it on GitHub https://github.com/square/dagger/issues/519#issuecomment-186246542.

JakeWharton commented 8 years ago

There is no leak. If the activity has outgoing references to dependencies and there are no incoming references to the activity it will be garbage collected.

jemshit commented 8 years ago

Ok, so the problem was Presenter keeping ref to Activity. Thanks :+1:

jemshit commented 8 years ago

One last question, after Activity is destroyed and so its Presenter is garbage collected, does Presenter still can hold references even it is garbage collected?

JakeWharton commented 8 years ago

No. Once garbage collected an object doesn't hold any references.

On Fri, Feb 19, 2016 at 2:07 PM Jemshit Iskenderov notifications@github.com wrote:

One last question, after Activity is destroyed and so its Presenter is garbage collected, does Presenter still can hold references even it is garbage collected?

— Reply to this email directly or view it on GitHub https://github.com/square/dagger/issues/519#issuecomment-186363609.

jemshit commented 8 years ago

I guess the thing i missed is Activity is destroyed does not mean it is garbage collected.