NSConnection sendSynchronousRequest中的内存问题

时间:2015-03-27 06:43:57

标签: ios memory httpclient nsconnection

我正在尝试在我的一个iOS项目中使用LolayHttpClient。除了一个问题之外它运行良好 - 即使我在autoreleasepool下运行代码,内存也会不断增长。

相关代码非常简单。 LolayHttpClient使用NSConnection sendSynchronousRequest。所以我怀疑内存问题是由sendSynchronousRequest的执行引起的。以下是我的测试功能。有人会帮忙吗?

int main(int argc, char * argv[]) {


        int max = 100000;
        while (max > 0) {

        @autoreleasepool {

            [NSURLCache setSharedURLCache:[[NSURLCache alloc] initWithMemoryCapacity:0
                                                                         diskCapacity:0
                                                                             diskPath:nil]];

            GetMethod *get = [[GetMethod alloc] init];
            NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:@"192.168.0.107:10000" path:@"/services"];

            HttpResponse *resp = [get executeSynchronouslyAtURL:url];



            max = max -1;
            NSLog(@"%@", [resp responseString]);
            NSLog(@"%@", [NSString stringWithFormat:@"%d",max]);
            [[NSURLCache sharedURLCache] removeAllCachedResponses];

            [NSThread sleepForTimeInterval:1];
        }

        }

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

}

executeSynchronouslyAtURL:url最终在同一个线程中调用executeMethodSynchronously,如下所示。

- (HttpResponse*)executeMethodSynchronously:(NSURL*)methodURL methodType:(NSString*)methodType dataInBody:(bool)dataInBody contentType:(NSString*)contentType error:(NSError**) error {

    //Create a new URL request object
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];

    if(cachePolicy != NSURLRequestUseProtocolCachePolicy) {
        [request setCachePolicy:cachePolicy];
    }

    [request setHTTPShouldHandleCookies: handleCookies];

    [self prepareMethod:methodURL methodType:methodType dataInBody:dataInBody contentType:contentType withRequest:request];

    NSString* requestBody = [self bodyString];
    DLog(@"Request url=%@, headers=%@, parameters=%@, body=%@", [request URL], [self headers], [self parameters], requestBody.length < 4096 ? requestBody : [NSString stringWithFormat:@"(length=%lu)", (unsigned long) requestBody.length]);

    //Execute the HTTP method, saving the return data
    NSHTTPURLResponse * response;
    NSError* errorResponse = nil;
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&errorResponse];
    HttpResponse * responseObject = [[HttpResponse alloc] initWithHttpURLResponse:response withData:returnData];

    if (errorResponse) {
        DLog(@"Error url=%@, error=%@", [request URL], errorResponse);
        if (error != NULL) {
            *error = errorResponse;
        }
    }

    DLog(@"Response url=%@, status=%li, headers=%@, body=%@", [request URL], (long) [responseObject statusCode], [responseObject headerFields], [responseObject responseString]);

    return responseObject;
}

Xcode仪器工具指向功能:

HTTPNetStreamInfo :: _ readStreamClientCallBack(__ CFReadStream *,unsigned long)

定期调用,每次调用时分配132k字节。

0 个答案:

没有答案