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

Reduce NSURLSession memory footprint #969

Merged
merged 4 commits into from
Jun 19, 2018

Conversation

csabol-stripe
Copy link
Contributor

Summary

Don't create a new NSURLSession instance for each ephemeral key request

Motivation

https://jira.corp.stripe.com/browse/IOS-773

Creating NSURLSessions induces a memory overhead because iOS allocates and saves cache space (ref. https://developer.apple.com/library/archive/qa/qa1727/_index.html). We were also not properly invalidating old sessions as we replaced them, aggravating the problem.

The other option which appears to remove all memory leaks is to use the shared instance of NSURLSession, but we want to keep a private instance. Using an ephemeral configuration (see link below) can also remove any of the reported links, but does not suit our use case.

More references:

https://stackoverflow.com/questions/42780244/swift-3-urlsession-memory-leak
https://stackoverflow.com/questions/28223345/memory-leak-when-using-nsurlsession-downloadtaskwithurl/35757989#35757989
http://footle.org/2015/10/10/fixing-a-swift-memory-leak/

Testing

I used the standard integration demo and was able to easily reproduce the reported leaks just by viewing an item. With the changes all leaks attributed to the Stripe library were gone.

Adds method to add default stripe headers to any request
Setting the headers on requests instead of re-instantiating NSURLSession with new default headers avoids memory accumulation from the security framework (see https://developer.apple.com/library/archive/qa/qa1727/_index.html)
Copy link
Contributor

@danj-stripe danj-stripe left a comment

Choose a reason for hiding this comment

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

Looks good!

I think there're a couple changes left over from your investigation that can be removed.

@@ -160,7 +174,7 @@ - (void)setStripeAccount:(NSString *)stripeAccount {
_stripeAccount = stripeAccount;

// Regenerate url session configuration
self.urlSession = [NSURLSession sessionWithConfiguration:[self sessionConfiguration]];
// self.urlSession = [NSURLSession sessionWithConfiguration:[self sessionConfiguration]];
Copy link
Contributor

Choose a reason for hiding this comment

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

These should be deleted, not commented out.

return [additionalHeaders copy];
}

- (void)setUrlSession:(NSURLSession *)urlSession
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this needed/ever called? I saw you made the urlSession readonly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yeah, this is a vestige of a previous experimient. Guess I forgot to clean up the branch before making this PR :P

@@ -63,7 +63,7 @@ - (void)testInitWithConfiguration {
STPAPIClient *sut = [[STPAPIClient alloc] initWithConfiguration:config];
XCTAssertEqualObjects(sut.publishableKey, config.publishableKey);
XCTAssertEqualObjects(sut.stripeAccount, config.stripeAccount);
NSString *accountHeader = sut.urlSession.configuration.HTTPAdditionalHeaders[@"Stripe-Account"];
NSString *accountHeader = [sut configuredRequestForURL:[NSURL URLWithString:@"https://www.stripe.com"]].allHTTPHeaderFields[@"Stripe-Account"];
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Might be nice to extract this into a helper method on the test case.

Copy link
Contributor

@danj-stripe danj-stripe left a comment

Choose a reason for hiding this comment

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

🎉

@csabol-stripe csabol-stripe merged commit b17dd12 into master Jun 19, 2018
@csabol-stripe csabol-stripe deleted the csabol/IOS-773_memory_leaks branch June 19, 2018 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants