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 828627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:45:13+00:00 2026-05-15T03:45:13+00:00

I am learning Objective-C. I am trying to release all of the memory that

  • 0

I am learning Objective-C. I am trying to release all of the memory that I use. So, I wrote a program to test if I am doing it right:

#import <Foundation/Foundation.h>

#define DEFAULT_NAME @"Unknown"

@interface Person : NSObject
{
  NSString *name;
}
@property (copy) NSString * name;
@end

@implementation Person
@synthesize name;
- (void) dealloc {
  [name release];
  [super dealloc];
}
- (id) init {
  if (self = [super init]) {
    name = DEFAULT_NAME;
  }
  return self;
}
@end


int main (int argc, const char * argv[]) {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  Person *person = [[Person alloc] init];
  NSString *str;
  int i;

  for (i = 0; i < 1e9; i++) {
    str = [NSString stringWithCString: "Name" encoding: NSUTF8StringEncoding];
    person.name = str;
    [str release];
  }

  [person release];
  [pool drain];
  return 0;
}

I am using a mac with snow leopard. To test how much memory this is using, I open Activity Monitor at the same time that it is running. After a couple of seconds, it is using gigabytes of memory. What can I do to make it not use so much?

  • 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-15T03:45:14+00:00Added an answer on May 15, 2026 at 3:45 am

    Firstly, your loop is incorrect. +stringWithCString:… is not an +alloc/+new…/-copy method, so you should not -release it.

    Either one of these are correct:

    1. Don’t -release:

      str = [NSString stringWithCString: "Name" encoding: NSUTF8StringEncoding];
      person.name = str;
      
    2. Use -init:

      str = [[NSString alloc] initWithCString: "Name" encoding: NSUTF8StringEncoding];
      person.name = str;
      [str release];
      

    Similarly, in -[Person init]:

    - (id) init {
      if ((self = [super init])) {
        name = [DEFAULT_NAME copy]; // <----
      }
      return self;
    }
    

    Now, if you use variant #1, the memory should rise up to gigabytes as you have seen before, while variant #2 should be a rather constant, small value.

    The difference is because

    str = [NSString stringWithCString: "Name" encoding: NSUTF8StringEncoding];
    

    is equivalent to

    str = [[[NSString alloc] initWithCString:......] autorelease];
    

    An -autoreleased object means “transfer the ownership to the nearest NSAutoreleasePool, and let it release it later”.

    How late? By default, it’s when the current run loop ticked once. However, you did not have an explicit run loop here*, so the run loop did not run. The autorelease pool never have a chance to clear up these 109 allocated temporary strings.

    However, for variant #2, the temporary strings are immediately released, so the temporaries won’t fill up the memory. (We don’t need to wait for the pool to flush — there’s no pools involved.)


    Note:

    *: A run loop is a unique loop attached to each running thread. If you write a CLI utility, there’s seldom need to have a run loop.

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

Sidebar

Related Questions

I'm still learning about Objective-C memory management. I'm trying to implement several simple classes
I'm just learning to use XCode and program in Objective-C (my plan is to
I'm learning how to program, and starting off with Objective C. I'm trying to
First of all, I would like to say that I'm just beginning learning objective-c.
I'm learning Objective-C and trying to make a very simple command line calculator. 'S'
I'm learning Objective C and iOS development. I'm trying to recreate some of the
I'm just learning some basic programming in Objective C and Cocoa. I'm trying to
I'm new to Objective-c. For learning purposes I'm trying to build something like a
I'm learning Objective-C and trying to develop a simple zipper application, but I stopped
I'm working on learning Objective-C, and I'm trying to get a feel for the

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.