IOS - 内存管理 - 对NSURLConnection的递归调用导致内存使用量增加

时间:2015-06-30 19:00:39

标签: ios objective-c memory memory-leaks

我有一个函数可以递归调用,以根据最新日期从数据库中获取新数据。我的问题是这个递归调用导致内存使用量增加。我已经注释掉了代码以查看每次导致300kb被分配的行是什么,所以最新发生的是每次NSURLConnection被击中时我的内存使用量每10秒增加300kb。

这是递归调用的方法:

save()

这就是我对回复的处理方式:

-(NSString*)setupPhpCall:(NSString*)requestString :(NSString*)sciptPage{
@autoreleasepool {

    //NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:0];
    //[NSURLCache setSharedURLCache:sharedCache];


    NSHTTPURLResponse *urlresponse = nil;
    NSError *error = nil;
    NSString *response = @"";
    NSData *myRequestData = nil;
    NSMutableURLRequest *request = nil;
    NSData *returnData = nil;

    myRequestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];

     //Create your request string with parameter name as defined in PHP file
    request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat: @"http://www.hugt.co.uk/%@", sciptPage]]];
    // set Request Type
    [request setHTTPMethod: @"POST"];
    // Set content-type
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    // Set Request Body
    [request setHTTPBody: myRequestData];
    // Now send a request and get Response
    //NSHTTPURLResponse* urlResponse = nil;
    //NSError *error = nil;

    //if(tmpArray.count == 0){
        returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlresponse error: &error]; //THIS LINE CAUSES THE MEMORY USAGE TO INCREASE
        response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
    //}
    // Log Response
    urlresponse = nil;
    error = nil;
    myRequestData = nil;
    request = nil;
    returnData = nil;

     //NSLog(@"%@",response);/****/
    //[sharedCache removeAllCachedResponses];
    if(response != nil){

        return response;
    }

}
return nil;
}

0 个答案:

没有答案