How expensive is the creation of NSAutoreleasePool

I have a method that needs to be run on its thread 88 times per second (this is a callback for an audio unit.) Should I avoid creating an NSAutoreleasePool every time it calls?

+4
source share
4 answers

If you can avoid it, do it. If you can’t, you don’t need to worry about this, abstract pools are created and released quite quickly. If you need an exact answer, set up a simple test and measure (it is always a good idea when talking about performance).

+2
source

Creating the NSAutoReleasePool itself should not be too slow, but if there are many objects that will be deleted when you merge the pool, this may start slowly. It is probably worthwhile to profile how long the pool takes.

+5
source

Assuming you have just returned from Tools or Shark with concrete evidence that autocomplete pools are indeed a performance issue in your application ...

Creating your own abstracts pools is the answer to the dilemma. You do this when you create many objects, so as not to create too many at once and either enter the paging hell (on Mac), or receive a warning and / or end memory (iPhone OS).

But auto-detection pools are also objects. They are not free. The consumption of one pool of autoresists is tiny, but in the cycle where you create many objects, you probably create one pool for each object X, merging it and creating another one for the next objects X.

Even then, the pools of abstracts are probably not so many, and therefore they will not be much different. You should see this in your Tools or Shark application: most of the time spent in -[NSAutoreleasePool drain] , in turn, is spent in -[NSObject release] . This time you will spend whether you use the autonomy pool or not.

[ EDIT: As of December 2011, autoplay pools can now be created without an object using the @autoreleasepool . They are probably still not free (at least without ARC), but now they are even cheaper than before.]

Thus, the real solution in such cases is to simply create fewer objects. It could mean:

  • Using and reusing buffers, whenever possible, reallocating a previously used buffer when resizing is necessary. You can use the malloc_good_size function to round the size, to make it less likely that you need to redistribute (you can skip the redistribution if the old required size and the new required size are rounded to the same number). You can also consider only buffer growth, never decreasing it.
  • Use and reuse of mutable objects. For example, if you create a line and then write it to a document, rather than releasing it and creating a new one, delete its entire contents or replace all old contents with the first part of the β€œnew” line.
  • Setting the value of X (pool deletion threshold). Higher X values ​​mean shorter memory consumption, but fewer pools are created and selected. Lower X means more pools, but less risk of paging or receiving a memory alert. This can hardly make a big difference, unless you raise the X too much or lower it too high.
+4
source

See Mike Ash Comparison of General Operations Performance . When it was tested in 10.5, the creation and destruction of the abstract pool took 0.0003577 milliseconds.

+3
source

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


All Articles