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

Fix memory Leaks on iOS #1057

Merged
merged 10 commits into from
Oct 25, 2022
Merged

Fix memory Leaks on iOS #1057

merged 10 commits into from
Oct 25, 2022

Conversation

samirnen
Copy link
Contributor

@samirnen samirnen commented Sep 14, 2022

Observing significant leaks on iOS devices when using 1-DS to send telemetry. Making NSURLSession static instead of creating a new instance for every telemetry event resolved the issue of memory leakage. Attaching snapshots from instruments where memory usage is being tracked with a static NSURLSession and a non static one.

Screenshot 2022-09-14 at 14 44 15

Screenshot 2022-09-14 at 14 45 03

@lalitb
Copy link
Contributor

lalitb commented Sep 14, 2022

pasting here from the 1ds-sdk team's channel -

Hi team.

We are observing significant memory spiking and leaks with 1-DS on iOS devices. I'm attaching a snapshot from the leaks profile taken from instruments. I have tested with the fixes made in this Pull Request. As we can see in the snapshot the memory is spiking quite fast and leaking a significant amount as well.

On further analysis we have found out that for every 1-DS telemetry event we send, we are creating a new URL session. Instead of which if a static URL session is used, the leaks and spiking aren't observed anymore. Attaching a screenshot of the same.

Wanted to know if there is any reason behind having unique URL sessions for each telemetry request and if making the URL session static might cause any issues. Here is a link to the draft PR.


@lalitb
Copy link
Contributor

lalitb commented Sep 14, 2022

Adding @eduardo-camacho as reviewer, as he has written this part of iOS code.

Copy link
Contributor

@eduardo-camacho eduardo-camacho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see concurrency and multi-url problems with this change, please take a look.

@samirnen samirnen marked this pull request as ready for review September 21, 2022 06:15
@@ -29,6 +29,9 @@
return std::string("RESP-") + std::to_string(seq.fetch_add(1));
}

static dispatch_once_t once;
static NSURLSession* m_session = nil;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't these static's be member variables of HttpRequestApple ?

Copy link
Contributor Author

@samirnen samirnen Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I have tried to do the same. The HttpRequestApple class is defined in .mm file. I was getting a compilation error if I defined the static objects under the class inside .mm. A workaround for this is to define the HttpRequestApple class inside .hh header and define the static variables in the header itself. However that exposes the whole HttpRequestApple class to files which import the header. Hence I have created static objects outside of the class inside .mm file.

Copy link
Contributor

@lalitb lalitb Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we split it in the cc/h file, and ensure the source file is linked when building for iOS? Just a bit concerned with these global statics.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't these static's be member variables of HttpRequestApple ?

Indeed, this static variable could definitively be moved to the function itself as a local static.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samirnen - Do you think this is doable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried moving the variable to the function as a local static. But then the memory is leaking same as before. So didn't go ahead with that approach

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a create a new source file for the HTTPRequestApple class and have defined the static variables there

Copy link
Contributor

@eduardo-camacho eduardo-camacho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static variable could definitively be moved to the function itself as a local static

@samirnen
Copy link
Contributor Author

Hey @eduardo-camacho @lalitb can you please re review this PR

lib/http/HttpRequestApple.mm Outdated Show resolved Hide resolved

namespace MAT_NS_BEGIN {

static dispatch_once_t once;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are still global statics, but I understand there are challenges in making it member-variable for this class - https://stackoverflow.com/questions/13856037/can-i-declare-dispatch-once-t-predicate-as-a-member-variable-instead-of-static/13858628#13858628. Also, there are issues in making it static member of the class. Will approve it if it is not practically possible to do either.
Also I understand end to end tests have been done - events are flowing, and there is no memory leak observed in this part of code.

Copy link
Contributor

@eduardo-camacho eduardo-camacho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effectively this change is more disruptive than previous version when Lalit commented about the global static, and the global static thing is still present; am I missing something here? If we are going to allow the global statics in this case, then it's just cleaner how it was before splitting files. Would you agree @lalitb?

@@ -0,0 +1,34 @@
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be named HttpRequest_Apple.http (similar case with new .mm file) to match current platform specific naming scheme.

@lalitb
Copy link
Contributor

lalitb commented Oct 19, 2022

Effectively this change is more disruptive than previous version when Lalit commented about the global static, and the global static thing is still present; am I missing something here? If we are going to allow the global statics in this case, then it's just cleaner how it was before splitting files. Would you agree @lalitb?

Yes, I too feel so, the initial approach was much cleaner if we can't get rid of global statics with this approach. Sorry @samirnen if there are too many iterations :)

Satakarni Amirneni added 2 commits October 21, 2022 11:51
@eduardo-camacho eduardo-camacho merged commit b22500d into main Oct 25, 2022
@eduardo-camacho eduardo-camacho deleted the users/samirnen/memoryLeakFix branch October 25, 2022 16:07
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

Successfully merging this pull request may close these issues.

4 participants