objective c - Reducing the memory footprint of a function with a lot of autoreleased variables? -
i'm still wrapping head around of nuances of memory management in objective-c, , came following case i'm unsure about:
+ (nsdecimalnumber*)factorial: (nsdecimalnumber *)l { nsdecimalnumber *index = l; nsdecimalnumber *running = [nsdecimalnumber one]; (; [index intvalue] > 1; index = [index decimalnumberbysubtracting:[nsdecimalnumber one]]) { running = [running decimalnumberbymultiplyingby: index]; } return running; }
here decimalnumberbymultiplyingby , decimalnumberbysubtracting creating lot of nsdecimalnumbers, autoreleased understand it, worry until time containing program hanging unto awful lot of memory.
should introducing autorelease pool somewhere? (if where?) going have noticeable effect on performance (when compared side effect of memory in use)?
is autoreleasing right mechanism use here? should @ breaking loop apart , manually releasing memory i'm done it?
it's n00b question, i'm trying flavour best practice(s) is/are in situation.
it's practice avoid creating bunch of large autoreleased objects within single pass of run loop. seem aware of solutions. can use non-autoreleased objects , release them when you're done them. can create autorelease pool sections of code create large number of autoreleased objects. when object autoreleased, gets released when enclosing autorelease pool released/drained. using autorelease pool this:
nsautoreleasepool *subpool = [[nsautoreleasepool alloc] init]; // code generates bunch of autoreleased objects. [subpool release];
but, before optimization, run benchmarks , see if need optimize. guess method you've shown won't cause problems. however, let's you're trying apply method set of million random integers within single loop. in case, might benefit using autorelease pool.
check out apple's memory management programming guide cocoa more details.
Comments
Post a Comment