• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: ARC Retain Cycles
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: ARC Retain Cycles
      • From: Graham Cox <email@hidden>
    • Re: ARC Retain Cycles
      • From: ChanMaxthon <email@hidden>
    • Re: ARC Retain Cycles
      • From: Jens Alfke <email@hidden>
References: 
 >Re: ARC Retain Cycles (From: Jim McGowan <email@hidden>)

  • Prev by Date: Re: NSToolbarItem Action Not Passed to NSButton...
  • Next by Date: Re: NSComboBox and sorted list
  • Previous by thread: Re: ARC Retain Cycles
  • Next by thread: Re: ARC Retain Cycles
  • Index(es):
    • Date
    • Thread