Re: Why does the memory increase?
Re: Why does the memory increase?
- Subject: Re: Why does the memory increase?
- From: "Timothy J. Wood" <email@hidden>
- Date: Thu, 6 Jun 2002 11:59:17 -0700
On Thursday, June 6, 2002, at 07:41 AM, Lorenzo Puleo wrote:
Hi,
Before launching this task the memory was 5MB, and at the end of this
task
the memory remains at 9MB. Since I remove all the objects from the
instance
gItemsToCopyArray, it should come back to the initial value...
Please, may someone explain/fix this?
Because nothing is getting deallocated. You are overusing the
autorelease features of Foundation here.
You can do one of two things to make this code deallocate objects:
- Allocate an NSAutoreleasePool at the top of -StartTask and release it
at the bottom
- Better yet, use the non-autoreleased creation methods like so:
- (void)StartTask
{
int i;
gItemsToCopyArray = [[NSMutableArray alloc] init];
for(i=0; i < 50000; i++){
[self AddObjectToList];
}
// no need to remove the objects, this will happen automatically
when the array is deallocated
[gItemsToCopyArray release];
}
- (void)AddObjectToList
{
int i;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for(i = 0; i<10; i++){
NSNumber *number;
number = [[NSNumber alloc] initWithInt: i];
[dict setObject:number forKey:@"Name"];
[number release];
[gItemsToCopyArray addObject:dict];
}
[dict release];
}
- (void)StartTask
{
int i;
gItemsToCopyArray = [NSMutableArray arrayWithCapacity:0];
[gItemsToCopyArray retain];
for(i=0; i < 50000; i++){
[self AddObjectToList];
}
[gItemsToCopyArray removeAllObjects];
[gItemsToCopyArray release];
}
- (void)AddObjectToList
{
int i;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for(i = 0; i<10; i++){
[dict setObject:[NSNumber numberWithInt:i] forKey:@"Name"];
[gItemsToCopyArray addObject:dict];
}
}
Thank you
--
Lorenzo Puleo
mailto: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.