我正在使用 NSURLConnection 异步发布表单数据。使用工具我可以看到启动连接时内存使用量增加,但是一旦连接完成,内存使用量就会下降到高于之前基线的水平。我将此称为“泄漏”,因为在连接开始/结束后大约 60 秒,内存被释放并且图形回落到原始基线。
为什么这段记忆会被保留到这个时候?我怎样才能立即释放它?
- (id)initWithPostURL:(NSURL *)url content:(NSData *)content completionBlock:(void (^)(NSData *data))completionBlock boundary:(NSString *)boundary;
{
self = [super init];
if (self) {
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-type"];
[urlRequest setValue:[NSString stringWithFormat:@"%d", content.length] forHTTPHeaderField:@"Content-length"];
[urlRequest setHTTPBody:content];
_urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:NO];
[_urlConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[_urlConnection start];
_data = [NSMutableData data];
_dataSemaphore = NULL;
_completionBlock = completionBlock;
}
return self;
}