Re: +[NSData dataWithContentsOfURL:] leaks memory?
Re: +[NSData dataWithContentsOfURL:] leaks memory?
- Subject: Re: +[NSData dataWithContentsOfURL:] leaks memory?
- From: Daniel Jalkut <email@hidden>
- Date: Mon, 05 Dec 2005 22:11:24 -0500
Hi Mike! Nice to see you here.
I wouldn't let these memory readings cause you too much worry. I
can't exactly explain the trends you observe, but I don't want to
have to explain them, either! I assume there are minor
implementation details about the memory allocation system that you
should choose to "not be concerned with" unless you have a specific
reason to be. I ran your program and then ran the "leaks" command
against it. It revealed only one leak, of 1024 bytes, which I'm not
sure is even a true leak. I would use more sanctioned memory
management tools than "ps" -- I could easily see the fluctuations of
actual memory usage as being misleading in attempting to gauge your
true memory problems.
For example, I took your tool and added a loop criteria to the while
loop that it would run for 100 times. I let it go, and later looked
at the output. On the 1st loop, the memory usage was 51148. Later, it
was as high as 63076. By the 100th loop, the memory usage was back
down to 52796. I would consider all of this to be statistical noise.
Trust the leaks tool and MallocDebug, unless you have specific
reasons to worry that serious inefficiencies are escaping their grasp.
Daniel
On Dec 5, 2005, at 5:43 PM, Mike Morton wrote:
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:
sweater.com
This email sent to email@hidden
_______________________________________________
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