On Jan 19, 2010, at 9:21 PM, Shawn Rutledge wrote:

> On Tue, Jan 19, 2010 at 7:01 PM, mmalc Crawford <mmalc_li...@me.com> wrote:
>>> No, you should just *retain* the result of dictionaryWithCapacity.
>>> 
>> No, you shouldn't.
>> 
>> If you're writing an init method and want to assign the dictionary to an 
>> instance variable, you should use alloc/init.
> 
> Why?

The autorelease pool is convenient and in some cases necessary, but it does 
come at some cost.  It has some overhead in terms of time and memory, plus it 
tends to increase the peak memory usage of your program.  (Over time, assuming 
you've got all your memory management correct, a program's memory usage will 
come out the same, but autoreleased objects accumulate over the lifetime of the 
current pool, causing spiky memory usage.)

In general, it's always good to be aware of such trade-offs and make judgments 
with them in mind.  In a highly constrained environment like the iPhone, the 
recommendation is to avoid the autorelease pool in the simple cases where it's 
easy to do so.  Like, the difference between the following:

        someIvar = [[NSMutableDictionary dictionaryWithCapacity:5] retain];

and

        someIvar = [[NSMutableDictionary alloc] initWithCapacity:5];

is not hard on the developer, but keeps usage of the autorelease pool to a 
minimum.

Regards,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to