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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:21:26+00:00 2026-06-15T00:21:26+00:00

To test memory management and allocations, I wrote a simple Single View application, and

  • 0

To test memory management and allocations, I wrote a simple Single View application, and in viewDidAppear, and to create a long loop containing many objects, i wrote this:

- (void) viewDidAppear:(BOOL)animated
{
    NSDate * time = [NSDate date];
    [super viewDidAppear:animated];

    for (int i = 0; i < 20003; i++)
    {
        NSString * testString = [[NSString alloc] initWithString:@"This is a test string"];
        NSMutableArray * itemsArray = [[NSMutableArray alloc] init];
        for (int j = 0; j < 1000; j++)
        {
            [itemsArray addObject:testString];
        }
        if ((i % 1000) == 0)
        {
            NSLog(@"called %d", i);
        }
}
NSDate * time2 = [NSDate date];
NSTimeInterval interval = [time2 timeIntervalSinceDate:time];
[label2 setText:[NSString stringWithFormat:@"time interval: %f", interval]];

}

While profiling for memory leaks, as expected, there was memory leak and more than 260 MB of allocations, screenshot:
image 1

But when following this document, I changed the code to:

- (void) viewDidAppear:(BOOL)animated
{
    NSDate * time = [NSDate date];
    [super viewDidAppear:animated];

    for (int i = 0; i < 20003; i++)
    {
        @autoreleasepool
        {
            NSString * testString = [NSString stringWithFormat:@"%@", @"This is a test string"];
            NSMutableArray * itemsArray = [[NSMutableArray alloc] init];
            for (int j = 0; j < 1000; j++)
            {
                [itemsArray addObject:testString];
            }
            [itemsArray release];
            itemsArray = nil;

            if ((i % 1000) == 0)
            {
                NSLog(@"called %d", i);
            }
        }
    }
    NSDate * time2 = [NSDate date];
    NSTimeInterval interval = [time2 timeIntervalSinceDate:time];
    [label2 setText:[NSString stringWithFormat:@"time interval: %f", interval]];

}

there was still no difference in the amount of allocations, but there was no memory leak, and the time of execution increased significantly, from less than 2 seconds to around 80 seconds:
Image 2

And still after using release, there was no change. Code:

- (void) viewDidAppear:(BOOL)animated
{
    NSDate * time = [NSDate date];
    [super viewDidAppear:animated];

    for (int i = 0; i < 20003; i++)
    {
        NSString * testString = [[NSString alloc] initWithString:@"This is a test string"];
        NSMutableArray * itemsArray = [[NSMutableArray alloc] init];
        for (int j = 0; j < 1000; j++)
        {
            [itemsArray addObject:testString];
        }
        [testString release];
        testString = nil;
        [itemsArray release];
        itemsArray = nil;

        if ((i % 1000) == 0)
        {
            NSLog(@"called %d", i);
        }
    }
    NSDate * time2 = [NSDate date];
    NSTimeInterval interval = [time2 timeIntervalSinceDate:time];
    [label2 setText:[NSString stringWithFormat:@"time interval: %f", interval]];
}

And screenshot:
enter image description here

And emptying the MutableArray explicitly (using [itemsArray release]) showed no change.

My questions are:

  1. Why is the total allocated memory not changing in the three cases (despite no memory leak in second and third case), is there a way to reduce it for large allocations? Where is the fault and how to reduce the memory consumed?

  2. Second, am I using the right tools and reading the right numbers (using Memory Leaks in Profiler Instruments, and referring to All Allocations, and in allocation lifespan, created and still living is selected) and am I doing it the right way? I mean is this the amount of memory being consumed at the moment, or total memory consumed since the application started?

  3. Why there was a significant increase in the time of execution of the loop in second case, and not in 1st and third case?

Using xCode 4.5, no ARC, no StoryBoards, and all testing was done on the simulator.

  • 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-06-15T00:21:27+00:00Added an answer on June 15, 2026 at 12:21 am

    1. Total allocations will be the same between your three cases because in each case you are allocating the same amount of memory.

    2. You are using the right tools, and reading some of the right numbers, I think you are just not reading enough numbers. Overall Bytes is essentially, as you suspected, not what you are currently consuming. You’ll want to examine the Live Bytes section. In this section you will see the difference between your first case, which wastes nearly half the total allocations, and your second two, which ended up in a fairly slim state.

    3. The “significant” time increase comes from the differences between using:

    [[NSString alloc] initWithString:@"This is a test string"];
    

    and

    @autoreleasepool  -and-
    [NSString stringWithFormat:@"%@", @"This is a test string"];
    

    Yes I said differences. And not just autorelease. But…

    First autorelease is notoriously inefficient at releasing memory in loops. It wasn’t really its purpose from the start, it was only really invented so that methods could return objects to be owned by the caller of the object, in other words: It’s designed to make things live too long. While @autorelease has been shown to be much more efficient than the old NSAutoReleasePools , it should be avoided in situations like this.

    Second is a fairly unknown optimization done by NSString when it comes to the handling of string literals as the source string for a new NSString. As described in this answer, Using initWithString:@"SomeLiteral" results in a pointer to the literal and not the formation of a new NSString. This optimization does not apply to stringWithFormat:. So in other words, you have skewed your test.

    Notes:

    Calling [super viewDidAppear:animated]; after you save the start time needlessly skews the test.

    When I ran these tests, They ran, in profiler, at these times:

    • Example 1: 0.999s
    • Example 2: 1.526s
    • Example 3: 1.082s

    Im not sure how you went from just under 2 seconds to 80 seconds.

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

Sidebar

Related Questions

I created a simple program to test the retain/release methods in Objective-C memory management.
I want to test a program's memory management capabilities, for example (say, program name
i have some questions about objective-c's memory management, let's say: NSString * test= [[NSString
We are doing a memory test of an application on centos. What we notice
I want to extensively test some pieces of C code for memory leaks. On
I have some crash in a test application I'm creating and I'm pretty sure
In ARC, does it create a memory leak to alloc into a @property (strong)
I have the following code to test memory deallocation using a std::list container: #include
I am trying to write simple memory manager (I should really say memory tracker)
I have an image compression application that now has two different versions of memory

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.