• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
+[NSData dataWithContentsOfURL:] leaks memory?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

+[NSData dataWithContentsOfURL:] leaks memory?


  • Subject: +[NSData dataWithContentsOfURL:] leaks memory?
  • From: Mike Morton <email@hidden>
  • Date: Mon, 5 Dec 2005 17:43:55 -0500

All --

Our app loads a lot of data using +[NSData dataWithContentsOfURL:] and this seems to use up memory, even though it loads inside the scope of an autorelease pool.

Here’s a simple (I hope) example, which seems to grow without bound. Each time it prints its own memory size, the size has grown by 112Kb, exactly the size of the downloaded image.

A few years back (at http://www.cocoabuilder.com/archive/message/cocoa/2002/3/15/62361 ) some folks discussed whether +dataWithContentsOfURL: leaks, but I can’t find any workaround. Does anyone know if NSURLHandle works better?


(I’m not sure it always uses up memory. Among the many testbed tools I wrote while investigating this, I found some cases where the memory does seem to get freed — sometimes upon loading an image larget than the earlier ones. I figured I should stick with a simple example…)

-- Mike


#import <Foundation/Foundation.h>

static void printMemory (void)
{
printf ("memory = ");
fflush (stdout);

system ("ps guaxc | grep MemoryTest | awk '{print $5}'");
}

int main (int argc, const char * argv[])
{
NSAutoreleasePool *mainPool = [[NSAutoreleasePool alloc] init];

[[NSURLCache sharedURLCache] setMemoryCapacity: 0]; // doesn't help

NSString *urlString = @"http://www.jpl.nasa.gov/wallpaper/art/pia06890-1600-1200.jpg";;
NSURL *url = [NSURL URLWithString: urlString];
printf ("data length = %d\n", [[NSData dataWithContentsOfURL: url] length]);

while (1)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[NSData dataWithContentsOfURL: url];

[[NSURLCache sharedURLCache] removeAllCachedResponses]; // doesn't help

printMemory ();

[pool release];
}

[mainPool release];
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Follow-Ups:
    • Re: +[NSData dataWithContentsOfURL:] leaks memory?
      • From: Lee Morgan <email@hidden>
    • Re: +[NSData dataWithContentsOfURL:] leaks memory?
      • From: Daniel Jalkut <email@hidden>
  • Prev by Date: Re: Do bindings "pay attention" to visibility of control?
  • Next by Date: Re: Search Array for Variable in Object
  • Previous by thread: Scroller partially visible in NSView
  • Next by thread: Re: +[NSData dataWithContentsOfURL:] leaks memory?
  • Index(es):
    • Date
    • Thread