iOS プロジェクトの 1 つで 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 バイトを割り当てます。