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

  • SEARCH
  • Home
  • 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 6117051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:16:35+00:00 2026-05-23T15:16:35+00:00

NSAutoreleasePool .. should there be only one? Why would you want to have more

  • 0

NSAutoreleasePool .. should there be only one? Why would you want to have more then one? Would you ever want to have more than one pool as part of a single class?

If you have multiple pools, which one will contain object that was asked for be [autorelease]d? Is there a way to find out what objects are waiting to be auto-released as part of a pool?

Sorry for 20 questions, please help me understand this better

  • 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-23T15:16:36+00:00Added an answer on May 23, 2026 at 3:16 pm

    NSAutoreleasePool .. should there be
    only one?

    No, not necessarily. You may create as many autorelease pools as you want/need.

    Why would you want to have more then
    one? Would you ever want to have more
    than one pool as part of a single
    class?

    The idea is to keep your memory “high water mark” as low as possible. Using autorelease is a bit of a cheat to defer releasing your object until “later”. Sometimes you know when “later” is — and in these cases, it’s probably smart to make your own autorelease pool.

    What do I mean by all this? Well, imagine you had the following loop:

    for (...)
    {
         // 1 MB of objects are added to the autorelease pool by some code...
    }
    

    1 MB is a lot! If that code looped 20 times, you’d have 20MB of objects waiting to get released. Even worse, if it ran for an indefinite or indeterminate number of times your application may very well crash. If you know that code is self-contained you can force anything that gets put into an autorelease pool within that block to get released by creating your own autorelease pool manually, like so:

    for (...)
    {
         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
         // 1 MB of objects are added to the autorelease pool by some code...
         [pool drain];
    }
    

    Now your “high water mark” is only 1MB instead of 20MB (or more!).

    If you have multiple pools, which one
    will contain object that was asked for
    be [autorelease]d?

    The most recent one.

    Imagine having a global stack. When you init a new AutoreleasePool, it adds itself to this global stack of autorelease pools. When you call [xxx autorelease] on an object, the autorelease method peeks at the autorelease pool on the top of this stack and adds itself to that autorelease pool’s list of objects. When you call [pool drain], that pool loops through all of the references that have been added to it and it calls [xxx release] on all of them.

    As BJ Homer points out, the stack in the paragraph above isn’t actually truly global – there is actually one stack per thread. But I couldn’t figure out how to rewrite the above paragraph and keep it easily understandable by using terms like “thread-local”… so… this addendum will have to suffice : )

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

Sidebar

Related Questions

I have a crash taking place when an NSAutoreleasePool drains. Presumably the pool is
Let's say that I have a code like this: NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init]; for(unsigned int
I have this in my main.m: printf(make autorelease pool \n); NSAutoreleasePool * pool =
I have the following piece of code in displaying the mapview. NSAutoreleasePool *pool =
I have the following method: +(NSMutableDictionary *)getTime:(float)lat :(float)lon { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]
So I have a question about auto-release pool. I created one use it as
My test code: NSManagedObjectContext *context; ..... NSAutoreleasePool *pool = [NSAutoreleasePool new]; User *u =
I call a function with performSelectorInBackground, and in this function, I declare NSAutoreleasePool *pool
I have an UIActivityIndicator that should loop until a PDF file is loading. I
I tried below code : NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *

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.