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

The Archive Base Latest Questions

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

I have managed to get myself confused about some elements of memory management. I

  • 0

I have managed to get myself confused about some elements of memory management. I am new to objective-c, and memory managed languages generally.

I have read the memory management guidelines, but I remain confused about a couple of things.
1) Is there any need to clean up ivars, and method variables that are not retained by any object. For instance

-(void) someMethod{
int count = 100;
 for (int i=0; i <count;  i++) {
  NSLog(@"Count = %d", i);
 }
    }    

What happens to the “count” var after the method is complete?
If a method allocates lots of temporary variables, do those get removed from memory as long as they are not unreleased, alloc’d objects? Or do i need to set them to nil in some way?

2) If i have a static variable, for instance an NSString, do I have to do anything for that to be removed from memory when the class is dealloced?

3) I have noticed that NSStrings seem to have a retainCount of 2147483647 which wikipedia tells me is the max value for a 32 bit signed integer.

http://en.wikipedia.org/wiki/2147483647

myString retainCount = 2147483647

-(void) someMethod{
NSString *myString = @"testString";
NSLog(@"myString retainCount = %d", [myString retainCount]);
// logs:  myString retainCount = 2147483647
}

What happens to this at the end of the method? Does this memory ever get emptied? The string is not being referenced by anything. My understanding is that the @”” convenience method for NSString returns an autoreleased object, but whats the point of autoreleasing something with a retainCount of 2147483647 anyway? In that case, whats the point of retaining or releasing ANY NSString?

I am well aware that retainCount should be ignored, but it just bugs me not to know what’s going on here.

4) Does this matter at all? I know that the memory associated with an NSString isn’t much to write home about, but I want to be a good memory management citizen, and I’m more interested in best practices than anything else.

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

    Retain/release only matters for objects, not int, bool, float, double or other built-ins. So use it for id or other classes that you hold a pointer to an object. In your example, count doesn’t need to be retained or released. It is allocated on the stack which is automatically cleaned up when the function returns.

    You do need to deal with any local objects you alloc. Those are created with a retainCount set to 1, so you need to either hold on to them for later or release them. Most Cocoa functions (that don’t start with copy or alloc) return an object that is autoreleased. This means that they will have release called on them later — you can only hold these after the function if you call retain on them. If you want them to be cleaned up, you don’t need to do anything (calling release would result in too many release calls).

    If you have a static variable pointing to an object, then it is not touched when objects of that class are dealloced. If you want it to be released, you have to call release on it. If the static is an int, bool, or other built-in, you don’t (can’t) call release on it. That’s part of the global memory of your app.

    NSStrings that are set to string literals should not have release called on them. The retainCount is meaningless for them. That value is also -1 for signed int values.

    If you do this — [[NSString alloc] initCallHere:etc] — you have to call release on it. Most of the time you get strings, you don’t use alloc, so you don’t need to call release. If you retain one, you need to call release.

    Yes, it does matter. Over time leaks will cause the iPhone to kill your app.

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

Sidebar

Related Questions

I have successfully managed to get System.Speech.Synthesis to read English text in arbitrary voices
I managed to get some help from a previous question and I have the
I have managed to get a cron job to run a rake task by
Ok so I have managed to get the format of the date presented in
So far I have managed to get the background image to stretch: XHTML: <div
I am building a client's site in WP and have managed to get the
I am learning basics of EJB 3.0. I have managed to get a sample
I'm writing a MVC website and using jqGrid. I have managed to get it
I have finally managed to get javan-whenever gem working on my site5 server, and
I have somehow managed to get a really screwed up Eclipse project. I have

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.