Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 223259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:12:15+00:00 2026-05-11T19:12:15+00:00

(I have read Apple’s memory management guide, as well as other memory management help

  • 0

(I have read Apple’s memory management guide, as well as other memory management help here, but am still confused on the following)

What should I do for memory management with convenience methods in a loop? Do I need to explicitly create an autorelease pool and then drain it. Or is it all automagic?

e.g.

for (i=0; i<numFilePaths; i++) {
    // ...
    NSString *componentString = [someString lastPathComponent];
    // ...
}

In this example, I’m repeatedly getting a new string from lastPathComponent. Ignoring for a moment the bad manners in the same thing repeatedly, how should I be handling this memory management situation?

‘componentString’ is not retained at all, it has a lifespan only within the loop, and is used merely for comparisons with other strings. Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:12:16+00:00Added an answer on May 11, 2026 at 7:12 pm

    The autorelease pool only gets drained when the run loop finishes, usually at the end of a method when control returns to the user. If you find yourself running through a loop many times which will result in the creation of a lot of autoreleased objects (which will build up in memory until your method ends), you might want to create a local autorelease pool and drain it yourself.

    You can create and drain a local autorelease pool by just instantiating and releasing a new one.

    for (i=0; i<10000; i++) {
      NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
      NSString *aString = [NSString string];
      [pool drain];
    }
    

    There is likely a performance hit if you create and release too many pools, so you may want to check for certain number of iterations (say every 100 loops or so) to create and release the pools. There’s no set number, so you’ll have to do some playing around.

    UPDATE: Updated [pool release] to [pool drain] to maintain future compatibility with garbage collection on Marc Charbonneau’s recommendation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding memory management in objective-c. I have read Apple's various
I am a newer to objective c. I have read the memory management document
I have read the Apple documentation. They list the apps registered, yet in their
From what i have read apple doesnt expose the api to allow developers to
I have read the Core Animation Cook Book and Core Animation Programming Guide from
I've read Apple's Your first iOS application guide and everything there seems crystal clear
I have read the Apple docs - http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/TabBarControllers/TabBarControllers.html#//apple_ref/doc/uid/TP40007457-CH102-SW1 about creating TabBar programmatically. I want
I have recently read Apple's sample code for MVCNetworking written by Apple's Developer Technical
I have read through several reviews on Amazon and some books seem outdated. I
I have read a lot that LISP can redefine syntax on the fly, presumably

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.