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:

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:

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:

And emptying the MutableArray explicitly (using [itemsArray release]) showed no change.
My questions are:
-
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?
-
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?
-
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. 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:
and
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
@autoreleasehas been shown to be much more efficient than the oldNSAutoReleasePools , it should be avoided in situations like this.Second is a fairly unknown optimization done by
NSStringwhen it comes to the handling of string literals as the source string for a newNSString. As described in this answer, UsinginitWithString:@"SomeLiteral"results in a pointer to the literal and not the formation of a newNSString. This optimization does not apply tostringWithFormat:. 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:
Im not sure how you went from just under 2 seconds to 80 seconds.