CFNetwork memory spike when fetching image

2020-07-18 09:39发布

问题:

I'm profiling my app in Instruments, and have found a troubling amount of allocations by CFNetwork when I use AFNetworking to fetch a remote image.

In one instance I saw 89 MB of allocations (the image is only around 200 KB in size). I've attached a screenshot to demonstrate. Has anyone else noticed this before? The memory is released appropriately but I can't understand why this happens in the first place.

Thanks in advance.

回答1:

I'm seeing the same issue on iOS 8 ONLY. It doesn't happen on any version of iOS 7. On simulator its 132kb, on a device its 144kb.

It seem's to be related to this issue:

https://github.com/AFNetworking/AFNetworking/issues/2314

I'm performing a http get using nsurlsession in this example. If you hit a place that doesn't send keep-alive, ie google.com, it wont malloc those kb. But if you hit a place that sends keep-alive, it will.

NSString * url = [NSString stringWithFormat:@"%@%.20f", @"http://someplace.com/?test=", (double)[[NSDate date] timeIntervalSince1970];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:nil] resume];