+[NSData dataWithContentsOfURL:] leaks memory?
+[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