Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak #15

Closed
supervital opened this issue Jul 5, 2015 · 4 comments
Closed

Memory leak #15

supervital opened this issue Jul 5, 2015 · 4 comments

Comments

@supervital
Copy link

Hi Kaushik, thanks for yours examples about RxJava. I`ve just started learning it. I investigated RxJava with Retrofit, and found an issue. When the request being done to server, an exception is thrown with 403 error. onError called, an exception logged. looks Ok.
The issue is - when I close the app with back button, and open again, and do the request again, exception is thrown, and 2 errors was logged. Do the same steps, onError was called, 3 erros was logged, etc. Memory leak occurs.

Firstly - it seems strange, the Observable finished with an error, onError was called, app was closed with back button, onDestroy was called, all objects should be garbage collected.Isn't it ?

Ok, I tried to call unsubscribe on OnDestroy, the issue still reproduce.
I googled the issue and found workaround using WeakReference. It looks awkwardly.

What is your opinion about this ? Or, I did something wrong ?

@kaushikgopal
Copy link
Owner

Hiya.

Sure let's try to break it down and get to the bottom of the issue.

The first problem you described seems like your not unsubscribing from the
observable at the end of the life cycle. Help me answer these questions:

  1. Did you pull the latest code from the repo? (I recently pushed some
    fixes)
  2. Which example specifically are you trying ? Give me the name of the
    class from the sample repo.

@supervital
Copy link
Author

  1. No, I didn`t.
  2. I was trying the RetrofitFragment.

The interesting thing is that I unsubscribe on OnDestroy of that fragment. In the example the Observable was not unsubscribed, so, I created the local Subscriber, init it by button click, and unsubscribe on OnDestroy.

I have just pulled the changes and the issue still reproducing. I was debugging and the Observable is being unsubscribed.

Steps to reproduce:

  1. Turn off the internet (to simulate no internet connection exception)
  2. Open the Retrofit-RxJava example.
  3. Press the button.
  4. onError observer is called. Error logged.
  5. close the app with back button.
  6. do 2,3 steps
  7. two onError is called. 2 Error logged.

Kill the process - helps to fix the issue.
Looks like Observable holds the Activities reference, even app was closed with back button.

kaushikgopal pushed a commit that referenced this issue Jul 6, 2015
This was a red herring and not caused by the Rx activity.
Basic fragment construction + backstack was at fault
@kaushikgopal
Copy link
Owner

excellent catch! couple of points:

a. the "leak" was not being caused by any of the Rx activity. so the Retrofit + RxJava combination was not causing any of the issues. I tested this by removing all Rx related stuff from the fragment and placing a debug log statement in the onResume method. You'll notice multiple instances of the fragment being created.
b. while i cannot pinpoint and tell you exactly what went wrong, i cleaned up the fragment construction and the backstack behavior. This solved the problem. If you have a look at #a0b2e2d you'll see the differences. Essentially i remove the useless activity_main layout, and when adding fragments i use android.R.id.content instead of a custom id.

Needless to say, post cleanup, the multiple fragment instance logs didn't show up.

i'm going ahead and closing this issue. thanks for letting me know. if you're still facing issues, let me know and i'll reopen.

@supervital
Copy link
Author

Yes, the issue is resolved.

Thanks for your contribution to RxJava tutorials! The examples are clear and useful! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants