Re: NSAutoreleasePool: how does it really work?
Re: NSAutoreleasePool: how does it really work?
- Subject: Re: NSAutoreleasePool: how does it really work?
- From: Oleg Svirgstin <email@hidden>
- Date: Tue, 18 Mar 2003 13:15:26 +0300
Hi,
In
>
===============================
>
- (void)MyLoop
>
{
>
int i;
>
xArray = [NSMutableArray arrayWithCapacity:0];
>
for(i = 0; i < 500000; i++){
>
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
>
NSString *sourceItem = [[NSString alloc]
>
initWithFormat:@"Item number %d", i];
>
[sourceItem autorelease];
>
[xArray addObject:sourceItem];
>
[myPool release];
>
}
>
[xArray removeAllObjects];
>
}
>
===============================
the pool releases NOTHING but itself, because all items get created with
retain count = 1, then they get scheduled for autorelease (retCount--) and
then retained by the NSArray (retCount++).
I don't know how this can result in memory leaks, though...
Any way, this code is NOT the same as the one that does not produce leaks
because
(1) [xArray addObject:[NSString stringWithFormat:@"Item number %d", i]];
(actually, it does all the same the above fragment)
is significantly different from
(2) [xArray addObject:@"abc"];
I would suggest such experimental pair:
>
===============================
>
- (void)MyLoop1
>
{
>
int i;
>
xArray = [NSMutableArray arrayWithCapacity:0];
>
for(i = 0; i < 500000; i++)
>
{
>
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
>
[xArray addObject:[NSString stringWithFormat:@"Do leak, step %d", i]];
>
[myPool release];
>
}
>
[xArray removeAllObjects];
>
}
>
===============================
against
>
===============================
>
- (void)MyLoop2
>
{
>
int i;
>
xArray = [NSMutableArray arrayWithCapacity:0];
>
for(i = 0; i < 500000; i++){
>
[xArray addObject:[NSString stringWithFormat:@"Don\'t leak, step %d", i]];
>
}
>
[xArray removeAllObjects];
>
}
>
===============================
and then compare results. IMHO, they BOTH should not leak. :(
Regards
Oleg
>
I don't understand yet how does NSAutoreleasePool really work.
>
At the end of the following loop, the "top" command tells me that the Rsize
>
of my application doesn't decrease anymore. Instead I expect that it should
>
return to the initial value because I remove all the objects from the
>
xArray. I get lack of memory all the time. I know that "addObject" retains
>
the object just added, but, how to free up the memory at the end?
>
>
===============================
>
- (void)MyLoop
>
{
>
int i;
>
xArray = [NSMutableArray arrayWithCapacity:0];
>
for(i = 0; i < 500000; i++){
>
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
>
NSString *sourceItem = [[NSString alloc]
>
initWithFormat:@"Item number %d", i];
>
[sourceItem autorelease];
>
[xArray addObject:sourceItem];
>
[myPool release];
>
}
>
[xArray removeAllObjects];
>
}
>
===============================
>
>
>
>
If I use only:
>
===============================
>
- (void)MyLoop
>
{
>
int i;
>
xArray = [NSMutableArray arrayWithCapacity:0];
>
for(i = 0; i < 500000; i++){
>
[xArray addObject:@"abc"];
>
}
>
[xArray removeAllObjects];
>
}
>
===============================
>
it works well: at the end of the loop, the Rsize returns to the initial
>
value.
>
>
Where can I learn more about NSAutoreleasePool? I already read the
>
documentation, but it seems to be not enough. And what did I do wrong?
>
>
>
>
Best Regards
>
--
>
Lorenzo Puleo
>
email: email@hidden
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.