Creating Auto-Detect Objects in iPhone Development

I have a requirement to create several NSDecimalNumber object objects as part of my application (since I need the precision of the calculation they offer), but I note that in the calculations they return NSDecimalNumber objects that are supposedly being implemented.

My question is whether this is really potentially problematic in an iPhone application where I can do a lot of calculations.

The question is related not only to NSDecimalNumber, but also to the inevitable creation of auto-implemented objects in the process of developing an iPhone application.

Any detailed answers to this question would be greatly appreciated.

+1
source share
4 answers

Yes, creating a large number of instances with auto-implementation on the iPhone can create memory problems, especially in a closed loop, so I try to avoid them when I can. You can create your own auto-detection pools to manage this, but they will also add some performance overhead and additional code that you need to track.

It is for this reason that when I perform precision calculations, I prefer to use NSDecimal C struct instead of NSDecimalNumbers. In fact, I performed some tests on this issue and found a significant performance increase when working with the C-structure (copied from my answer here ):

 NSDecimal Additions per second: 3355476.75 Subtractions per second: 3866671.27 Multiplications per second: 3458770.51 Divisions per second: 276242.32 NSDecimalNumber Additions per second: 676901.32 Subtractions per second: 671474.6 Multiplications per second: 720310.63 Divisions per second: 190249.33 

As you can see, almost 5 times the computational speed between NSDecimal paths and NSDecimalNumber. The biggest difference between NSDecimal and NSDecimalNumber calculations was the memory allocation of NSDecimalNumber instances. Therefore, you should avoid allocating temporary instances with auto-implementation, wherever you are.

+7
source

Remember that you can create your own NSAutoreleasePool objects.

For instance:

 for (int i = 0; i < 1000; ++i) { NSAutoreleasePool * p = [[NSAutoreleasePool alloc] init]; for (int j = 0; j < 1000; ++j) { NSString * s = [NSString stringWithFormat:@"%d,%d", i, j]; NSLog(@"%@", s); } [p release]; } 

If you do this, you will never have more than 1000 such lines in memory at a time.

+8
source

If you are worried about working with too many auto-implemented objects, you can create your own autostart pool (see memory management ):

 for (count = 0; count < limit; count++) { NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init]; NSString *fileContents; NSString *fileName; fileName = [args objectAtIndex:count]; fileContents = [[[NSString alloc] initWithContentsOfFile:fileName] autorelease]; // this is equivalent to using stringWithContentsOfFile: /* Process the file, creating and autoreleasing more objects. */ [loopPool release]; } 
+3
source

Each object that is given a piece of memory will have to abandon it. The question is when.

I am trying to alloc/init/release when I can, so that objects freeze only as long as they are needed.

If I use autorelease , I have less authority when releasing the object. If the application tries to access the released object, it may work. I think that tighter memory management is good.

As long as you save the auto-implemented objects returned by the method, you should be fine. (If you do not ask about something else, in this case I apologize in advance.)

0
source

Source: https://habr.com/ru/post/1333224/


All Articles