Reducing the memory of a function with many auto-implemented variables?

I am still enveloping some of the nuances of memory management in objective-C, and came up with the following case, which I'm not sure about:

+ (NSDecimalNumber*)factorial: (NSDecimalNumber *)l { NSDecimalNumber *index = l; NSDecimalNumber *running = [NSDecimalNumber one]; for (; [index intValue] > 1; index = [index decimalNumberBySubtracting:[NSDecimalNumber one]]) { running = [running decimalNumberByMultiplyingBy: index]; } return running; } 

Here, decimalNumberByMultiplyingBy and decimalNumberBySubtracting will create a lot of NSDecimalNumbers, which will eventually get auto-implemented, but I'm still worried that the containing program will hang up to a huge amount of memory.

Should I ever introduce a pool of abstracts? (If where is this?) Will this have a noticeable effect on performance (compared to the side effect of using a lot of memory)?

Is autoreleasing the correct mechanism to use here? Should I watch how to break the loop and manually free the memory, how did I do it?

This is probably a n00b question, but I'm trying to get an idea that the best practice is in this situation.

+4
source share
3 answers

It’s good practice to avoid creating many large objects with auto-implementation during one run of the run loop. You already know about the solutions. You can use unauthorized objects and free them when you're done with them. You can also create an autoplay pool for sections of your code that create a large number of objects with auto-implementation. When an object is auto-implemented, it is freed when the autoresist reservation pool is freed / merged. Using a resource pool will look like this:

 NSAutoReleasePool *subPool = [[NSAutoreleasePool alloc] init]; // Code that generates a bunch of autoreleased objects. [subPool release]; 

But before doing any optimization, run some tests and see if you really need to optimize. I assume that the method you showed will not cause any problems. However, suppose you are trying to apply your method to a set of a million random numbers in a single loop. In this case, you may find it useful to use the autorun pool.

Check out the Cocoa Apple Memory Programming Guide for more information.

+5
source

The best way to determine the answer is to write several methods and tests. I do not think this will be a problem, but NSDecimalNumbers will be maximum around ~ 100 !, and 100 NSDecimalNumber objects will probably not matter much.


Answer other questions: in situations where it matters, you can manually free your objects. You can also create an autoplay pool in this loop. The abstract is super fast .

 while(/*condition*/) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // code goes here [pool drain]; } 
+1
source

You can configure the autostart pool in a loop, but why bother?

You will not be able to accumulate many objects in this cycle because you are calculating factorials, and the largest exponent NSDecimalNumber may be 127.

You will get an overflow error before you can get 100 iterations through the loop.

Please note that the main pool of autoresists will be launched each time the application touches the main launch cycle, so the values ​​of the autorealized value will not last very long.

+1
source

All Articles