Re: ARC Retain Cycles
Re: ARC Retain Cycles
- Subject: Re: ARC Retain Cycles
- From: Dave <email@hidden>
- Date: Sat, 26 Apr 2014 12:29:28 +0100
On 26 Apr 2014, at 08:27, Jim McGowan <email@hidden> wrote:
>
> On 25 April, 2014 5:45:24 pm HKT, Dave <email@hidden> wrote:
>
>> It’s not dogma! autorealease meant my App peaked at 150MB, whereas it needed 10 MB! Autorelease caused 140 MB of data to be useless held in memory for the duration of the Thread for no good reason.
>>
>
> ‘Autorelease’ wasn’t causing your app to use 150mb, it was your loop that was causing the memory usage.
Yes, because of autorelase! Take autorelase out of the equation and it doen’t use 150MB!
> If you have a loop where each iteration is going to create/fetch/copy a significant amount of data, then it is pretty standard practice to have an autorelease pool inside the loop, as otherwise you are depending on the next pool in the chain, which is probably the main thread’s pool which is only popped once per event loop interation.
Only if you use autorealease in your own code, which I don’t, and most of my code that interfaces to the OS is encapsulated so all I need do is drain the pool at this point. I don’t use autolrelease so I don’t need to worry about draining any pools!
>> You can’t avoid autorelease, you need to do some work to undo it ASAP to stop the memory building up too much. In this case the solution is to copy the data into new objects and then drain the pool. Works a treat - you can’t avoid it BUT you can undo it!
>
>
> I don’t really understand what you mean by “undo it”, but I the thing to remember is that autorelease pools are not like garbage collection, you have to be responsible for them in your own code in the same way that you are responsible for balancing new/init/copy/retain with release.
Except now, in ARC you don’t have to be responsible for balancing alloc/release, BUT in order to get the best out of the compiler its better to follow the new/alloc naming convention, since it stops ARC making the object auto releasable which was the first problem.
By undo, I meant if the OS (or whatever) returns an autoreleased object, I copy it into a +1 retained object, then drain the pool, and prefix the name of the method with new or alloc. So no one above this point in the call know or cares about autorelease,
Something like this:
-(NSString*) newThingFromOS
{
NSString* myString;
@autoreleasepool
{
myString = [[xxxxxx getAutoreleasedString] copy];
}
return myString;
}
Cheers
Dave
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden