microsoft / ApplicationInsights-Android

Microsoft Application Insights SDK for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

context and application leaked by ApplicationInsights

romainpiel opened this issue · comments

Hi!

I'm getting started with ApplicationInsights, interesting project. By looking at the source code I can see one problem with the way you deal with Android resources.

First, ApplicationInsights.setup() methods take a context. The reference to this context is then kept statically by the field INSTANCE. That's pretty bad and can lead to emory leaks. The documentation advises to use an Activity context, which is even worse than the Application context. See this post for more details about the problem. One solution would be to keep a weak reference to this context or add a setContext() to clear it up when the activity is released.
I noticed this context is only used by Telemetry and Persistence, why not adding this context when enabling these services? ApplicationInsights.setTelemetryEnabled(context).

Second, there's another memory leak problem with application. Moreover, the design could be improved on that side. It doesn't look obvious to me that passing an application to the setup() method initializes the LifeCycleTracking. In fact, the comment says @param application the application needed for auto collecting telemetry data but it's not used by the telemetry. Only by the LifeCycleTracking https://github.com/Microsoft/ApplicationInsights-Android/blob/master/applicationinsights-android/src/main/java/com/microsoft/applicationinsights/library/ApplicationInsights.java#L193-L199

Hi Romain
thanks for getting in touch and your feedback.
We were already aware of the issue with the leaking context and application end of last week.

I will provide another update later today that will also weakify the application.

About your design feedback: I will discuss with the team and see if we can incorporate it, or at least parts in this or the next sprint.

Feel free to ping us anytime with feedback, comments, or open a PR if you want/have something to contribute. :)

Cheers!
Benny

Hi Romain,

#36 fixes the issue with leaking the application object.

I have discussed your suggestions with the other teammates and we decided to not inject the context when we enable e.g. telemetry (something like you proposed, ApplicationInsights.setTelemetryEnabled(context)).
The reason for this is that we want every feature to be work out-of-the-box, so the dev only needs to call

ApplicationInsights.setup(getApplicationContext(), getApplication());
ApplicationInsights.start();

and crashreporting, life-cycle-tracking and telemetry are there and ready to use.
I hope this makes sense for you.

Cheers,
Benny