Re: Debugging memory leak in NSURLSession with ARC
Re: Debugging memory leak in NSURLSession with ARC
- Subject: Re: Debugging memory leak in NSURLSession with ARC
- From: Mike Abdullah <email@hidden>
- Date: Tue, 13 Jan 2015 09:11:13 +0000
> On 13 Jan 2015, at 01:00, Graham Cox <email@hidden> wrote:
>
> I'm still having an issue with this - I think.
>
> I've exhaustively hunted down every leak and memory allocation in my app - luckily it's a fairly small one, though one that can create many threads - and have eliminated everything I have control over*
>
> My heap space is still growing over time. I'm having a LOT of trouble understanding how to use Allocations Instrument effectively - I just can't really understand what it's trying to tell me. I have read the documentation for Allocations but it's not really much help, because it's hard for me to 'mark generations' when a 'generation' is something that happens as a result of external factors, not a user-interface action.
>
> I've satisfied myself that possible issues to do with blocks causing potential retain cycles are definitely not the issue, nor is fumbling my way using ARC for the first time**.
>
> What I'm left with is either a genuine memory leak that occurs inside the CFNetwork framework, or an apparent memory leak that isn't one really that I can safely ignore. I'm just not sure which.
>
> To recap, what my app does is to record chunks of video data to a file using NSURLSession and NSURLSessionDataTask. These chunks are obtained by parsing a .m3u8 playlist file. Each 'chunk' is a unique URL which is generated by the server and persists for a fairly short time. By concatenating the data returned by each fetch of these URLs, a complete capture of a live stream is achieved. Overall, this process works great with the nagging problem of growing memory usage. This growth appears to be somewhat related (but not exactly correlated with) the amount of data downloaded and recorded. For example, at first the "leak" is somewhat larger than what I record, but over time it becomes quite a bit smaller. However if I record nothing, there is no leak. What concerns me is that the "leak" can get large - after a day or so of running, it's getting up to 3GB for example.
>
> In my NSURLSessionConfiguration, I have turned off the URLCache (set it to nil) - there's no purpose to caching data for the chunks because they are only ever accessed once. Similarly I've disabled cookies. What I should be looking at is a straightforward uncached download of a URL, write that data to disk and move on. Here's my config setup:
>
> self.configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
> NSDictionary* additionalHeaders = @{@"User-Agent":XViPadUserAgentString};
> self.configuration.HTTPAdditionalHeaders = additionalHeaders;
>
> self.configuration.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever;
> self.configuration.HTTPCookieStorage = nil;
> self.configuration.URLCache = nil;
>
> self.session = [NSURLSession sessionWithConfiguration:self.configuration];
One small suggestion: I think the frameworks would prefer it if you copied the ephemeralSessionConfiguration, and then modified the copy. Modifying this shared config is probably not supported, and while it might work now, might not in future.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden