0

My app makes a lot of URL requests (calling a web service) in a loop for an extended period of time. When I watch the app in the Allocations tool, I see memory consumption going up continuously during the run of that loop. For testing purposes, I've reduced the loop to the following, which exhibits the same behavior:

NSURL *myUrl = [[NSURL alloc] initWithString:@"http://my.server.com/webservice"];
NSURLRequest request = [[NSURLRequest alloc] initWithURL:myUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
while (1)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSURLResponse *response = nil;
    NSError *error = nil;

    NSData *rawResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [pool release];
}

When I first discovered the problem, I figured it was because my URL requests/responses were being cached. That's when I added the [[NSURLCache sharedURLCache] removeAllCachedResponses]. I was expecting that to clear out the cache after each call to the web service and free up any memory being used for URL caching purposes. No luck.

I must be doing something wrong, but I can't find it. Am I barking up the wrong tree in thinking it's the URL caching? What else could it be?

Jeff Loughlin
  • 4,134
  • 2
  • 30
  • 47
  • 1
    You're creating 4 new variables per iteration of your loop. – nhgrif Oct 07 '13 at 14:20
  • @nhgrif: They're autoreleased, and I create an autorelease pool inside the loop. They go away on each iteration. – Jeff Loughlin Oct 07 '13 at 14:47
  • Just as a sanity check, are you sure you're looking at the correct metric in the allocation tool? You might be looking at the total amount of memory you app has allocated over its entire lifetime as opposed to the amount it has allocated at any given moment. – martega Oct 19 '13 at 10:41

0 Answers0