Re: Memory Management: Engine Example
Re: Memory Management: Engine Example
- Subject: Re: Memory Management: Engine Example
- From: John Stiles <email@hidden>
- Date: Sun, 01 Jan 2006 18:06:35 -0800
Will your engine parts take more than 500MB of memory?
If not, I wouldn't worry about it at all. Autorelease pools are flushed
nearly instantaneously (whenever events are processed) and for typical
apps, it's never ever a concern. You seem to be worrying about a
premature optimization.
Jordan Evans wrote:
I'm getting a lot of suggestions to put each
enginePart in a autorelease pool. But, wouldn't it be
better to cause these parts to immediately dealloc at
the time of replacement. Otherwise if my engine
becomes large enough and has a major overhaul, for
every new part replacement it will have a bad part
sitting in memory waiting to be autoreleased.
Potentially, couldn't this cause unnecessary problems?
If so, shouldn't -maintenance: just be totally
responsible for dealloc'ing the replaced parts, since
it's making the final crucial decision about their
existence?
Here's the new version with every attempt to follow
suggestions:
Source Code Attached.
#import <Foundation/Foundation.h>
//EnginePart Class
@interface EnginePart : NSObject
{
NSDate *dateCreated;
}
-(id)init;
-(NSDate*)creationDate;
@end
@implementation EnginePart
- (id)init
{
self = [super init];
if (self != nil)
{
dateCreated = [[NSDate date] retain];
}
return self;
}
- (void)dealloc;
{
[dateCreated release];
[super dealloc];
}
- (NSDate*)creationDate
{
return dateCreated;
}
- (NSString*)description
{
return [self className];
}
@end
//Individual Sub-Classes of EnginePart Class
@interface CombustionChamber : EnginePart{} @end
@implementation CombustionChamber @end
@interface Compressor : EnginePart{} @end
@implementation Compressor @end
@interface FuelInjector : EnginePart{} @end
@implementation FuelInjector @end
@interface Shaft : EnginePart{} @end
@implementation Shaft @end
@interface Turbine : EnginePart{} @end
@implementation Turbine @end
@interface Engine : NSObject
{
NSMutableArray *enginePartArray;
}
- (id)init;
- (void)maintenance:(NSMutableIndexSet*)badParts;
@end
@implementation Engine
- (id)init
{
self = [super init];
if (self != nil)
{
int i;
enginePartArray = [[NSMutableArray alloc] init];
[enginePartArray addObject:[[CombustionChamber
alloc] init]];
[enginePartArray addObject:[[Compressor alloc]
init]];
[enginePartArray addObject:[[FuelInjector alloc]
init]];
[enginePartArray addObject:[[FuelInjector alloc]
init]];
[enginePartArray addObject:[[Shaft alloc] init]];
[enginePartArray addObject:[[Turbine alloc] init]];
[enginePartArray
makeObjectsPerformSelector:@selector(release)];
}
return self;
}
- (void)dealloc;
{
[enginePartArray release];
[super dealloc];
}
- (void)maintenance:(NSMutableIndexSet*)badParts
{
int i, part, badPartCount;
id PartClass;
NSLog(@"Parts before maintenance:\n");
NSEnumerator *enumerator = [enginePartArray
objectEnumerator];
id aPart;
while (aPart = [enumerator nextObject])
NSLog(@"Part: %@ created: %@", aPart, [[aPart
creationDate] descriptionWithCalendarFormat:@"%S"
timeZone:nil locale:nil]);
part = [badParts firstIndex];
while (part != NSNotFound)
{
PartClass = [[enginePartArray objectAtIndex:part]
class];
[enginePartArray replaceObjectAtIndex:part
withObject:[[PartClass alloc] init]];
[[enginePartArray objectAtIndex:part] release];
part = [badParts indexGreaterThanIndex:part];
}
NSLog(@"Parts after maintenance:\n");
enumerator = [enginePartArray objectEnumerator];
while (aPart = [enumerator nextObject])
NSLog(@"Part: %@ created: %@", aPart, [[aPart
creationDate] descriptionWithCalendarFormat:@"%S"
timeZone:nil locale:nil]);
printf("\n");
}
@end
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]
init];
Engine *myEngine = [[[Engine alloc] init]
autorelease];
NSMutableIndexSet *badPartsIndexSet1 =
[[NSMutableIndexSet alloc] init];
NSMutableIndexSet *badPartsIndexSet2 =
[[NSMutableIndexSet alloc] init];
[badPartsIndexSet1 addIndex:0];
[badPartsIndexSet1 addIndex:1];
[badPartsIndexSet2 addIndex:5];
[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:2]];
[myEngine maintenance:badPartsIndexSet1];
[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:2]];
[myEngine maintenance:badPartsIndexSet2];
[badPartsIndexSet1 release];
[badPartsIndexSet2 release];
[pool release];
return 0;
}
__________________________________
Yahoo! for Good - Make a difference this year.
http://brand.yahoo.com/cybergivingweek2005/
------------------------------------------------------------------------
_______________________________________________
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
_______________________________________________
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