4

I have read so many blogs for @autorelease pool but I am still not clear about this @autorelease pool concept in objective c or ios development.

So can any one please throw a light on this concept. It would be very helpful to me if any one could provide me with an example of it and when the use of @autorelease pool is required.

How can I measure the memory allocation with use of @autorelease pool and without it.

Please throw a light on this topic might be it is duplicate.

dinotom
  • 4,990
  • 16
  • 71
  • 139
Darshan
  • 2,272
  • 3
  • 31
  • 43
  • [Here](http://stackoverflow.com/questions/9086913/objective-c-why-is-autorelease-autoreleasepool-still-needed-with-arc) it says why `Autoreleasepool` needed in ARC – byJeevan Apr 26 '16 at 06:32
  • i think its only useful when there are objects that are marked with __autoreleasing [(see this)](http://stackoverflow.com/questions/8862023/in-which-situations-do-we-need-to-write-the-autoreleasing-ownership-qualifier). so those kinds of objects will only be released when it hits the end of your apps main autorelease pool, but if many are created in a short amount of time, you may run out of memory, so you could strategically place your own autorelease pool around it so that the autoreleasing objects dont pile up and give you an out of memory error – Fonix Apr 26 '16 at 06:51
  • how can i measure memory utilizations in practical example when i use @autorelease pool and when i m not using it – Darshan Apr 26 '16 at 07:03
  • will have to look into using the xcode profiling tool for that, but 99% of the time you should not need an autorelease pool of your own, ARC usually takes care of things quite well (unless you make retain cycles which autorelease pools wont help with) – Fonix Apr 26 '16 at 07:21
  • Thanks for your quick reply :) – Darshan Apr 26 '16 at 07:24

1 Answers1

-2

Release and Autorelease are the terms related to the Memory Management. whenever you own a object its your responsibility to release it . if you don't release it properly, Objective -C cannot reclaim it for the use of other objects and there will be a memory leak.

Different ways to own a object are alloc, new , retain and copy Whenever you use this things try to release it so Objective C will take care blowing that object. If you are not sure about releasing that object, please make sure you do autorelease.

Whenever you do autorelease of an object the object is not released right way, it will be added to the Autoreleasepool in the main function. The Autoreleasepool in the main function will maintain a stack of objects to be released and they are released one by one when "drain"method is called eg: [pool drain]. Drain method is called repeatedly at the end of every event loop.

Anuj J
  • 186
  • 1
  • 11
  • 2
    nice copy paste https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html – Fonix Apr 26 '16 at 06:40
  • And answers are supposed to be answering the question and not linking. – gnasher729 Apr 26 '16 at 07:43