使用NSURLConnection时增加内存

时间:2014-03-28 16:05:14

标签: objective-c

我正在尝试使用NSURLConnection对服务器发出连续的帖子请求。 但我已经注意到,即使在我停止连续垃圾邮件之后,我的记忆也在不断增加,而且并没有下降。 一段时间后,应用程序将崩溃。

谁能告诉我这里做错了什么,以及如何解决这个问题? 我在这里使用ARC。

提前致谢

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(continuousCall) userInfo:nil repeats:YES];
}

-(void) continuousCall {
    NSString * json = @"{\"a\":{\"a1\":{\"3\":{\"value1\":0,\"value2\":255,\"value3\":72},\"1\":{\"value1\":255,\"value2\":234,\"value3\":0},\"2\":{\"value1\":0,\"value2\":255,\"value3\":72},\"0\":{\"value1\":0,\"value2\":255,\"value3\":72}},\"a2\":{\"7\":{\"value1\":0,\"value2\":255,\"value3\":72},\"3\":{\"value1\":0,\"value2\":255,\"value3\":72},\"8\":{\"value1\":0,\"value2\":255,\"value3\":72},\"4\":{\"value1\":255,\"value2\":234,\"value3\":0},\"0\":{\"value1\":0,\"value2\":255,\"value3\":72},\"5\":{\"value1\":255,\"value2\":234,\"value3\":0},\"1\":{\"value1\":0,\"value2\":255,\"value3\":72},\"6\":{\"value1\":0,\"value2\":255,\"value3\":72},\"2\":{\"value1\":0,\"value2\":255,\"value3\":72}},\"a3\":{},\"a4\":{\"3\":{\"value1\":0,\"value2\":255,\"value3\":72},\"1\":{\"value1\":0,\"value2\":255,\"value3\":72},\"2\":{\"value1\":255,\"value2\":234,\"value3\":0},\"0\":{\"value1\":0,\"value2\":255,\"value3\":72}}}}";

    [self connectWith:json];
    json = nil;
}

-(void) connectWith: (NSString*) json {
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    // Do any additional setup after loading the view, typically from a nib.
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:url] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:0];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[json dataUsingEncoding:NSUTF8StringEncoding]];
    //    [request addValue:nil forHTTPHeaderField: @"Content-Type"];

    //#1
    //    NSURLConnection * urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

    //#2
    NSURLConnection * urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
    [urlConnection start];

    //#3
    //    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    //    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    //        NSLog(@"%@", response.description);
    //        [[NSURLCache sharedURLCache] removeAllCachedResponses];
    //    }];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
    connection = nil;
    //    connection
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2 个答案:

答案 0 :(得分:0)

我发现了这个问题,它与NSURLConnection无关,我认为这个问题很难解决。 我正在使用JSONKit库,由于某种原因,CFString的内存不断堆积。我正在使用NSJSONSerialization。

答案 1 :(得分:0)

如果您正在做一些创建大量临时对象的事情,您可能希望创建一个新的NSAutoreleasePool池,如下所示:

-(void) continuousCall {
    id myPool = [NSAutoreleasePool new];

    NSString * json = @"{\"a\":{\"a1\":{\"3\":{\"value1\":0,\"value2\":255,\"value3\":72},\"1\":{\"value1\":255,\"value2\":234,\"value3\":0},\"2\":{\"value1\":0,\"value2\":255,\"value3\":72},\"0\":{\"value1\":0,\"value2\":255,\"value3\":72}},\"a2\":{\"7\":{\"value1\":0,\"value2\":255,\"value3\":72},\"3\":{\"value1\":0,\"value2\":255,\"value3\":72},\"8\":{\"value1\":0,\"value2\":255,\"value3\":72},\"4\":{\"value1\":255,\"value2\":234,\"value3\":0},\"0\":{\"value1\":0,\"value2\":255,\"value3\":72},\"5\":{\"value1\":255,\"value2\":234,\"value3\":0},\"1\":{\"value1\":0,\"value2\":255,\"value3\":72},\"6\":{\"value1\":0,\"value2\":255,\"value3\":72},\"2\":{\"value1\":0,\"value2\":255,\"value3\":72}},\"a3\":{},\"a4\":{\"3\":{\"value1\":0,\"value2\":255,\"value3\":72},\"1\":{\"value1\":0,\"value2\":255,\"value3\":72},\"2\":{\"value1\":255,\"value2\":234,\"value3\":0},\"0\":{\"value1\":0,\"value2\":255,\"value3\":72}}}}";

    [self connectWith:json];
    json = nil;

    // drain the new pool
    [myPool drain];
}