My app runs in Instruments taking up approximately 700 KB of Live Bytes on average while running. However, every time it loads a new full-screen image, the memory allocations jump about 10 MB for a second, and then recover to the normal 700 KB level.
This is okay at the beginning, but once it has happened a few times I receive memory warnings and the app quits, even though the total Live Bytes stabilises well under the 1 MB mark.
I have created a test project to see why this is happening. It is a Single View Application with only the following code in the View Controller:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *imgFile = [[NSBundle mainBundle] pathForResource:@"00-bg" ofType:@"png"];
UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgFile];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:img];
[img release];
[self.view addSubview:backgroundImageView];
[backgroundImageView release];
}
The output from Instruments (Leaks) looks like this:

I have tried both ARC and non-ARC, and the only difference is the length of the spike (ARC seems to hold onto the memory for longer).
I have also tried both UIImage imageNamed: and initWithContentsOfFile: but the results are the same.
Why is this spike happening? And is there anything I can do to avoid it?
To display them on screen, iOS has to uncompress your images and that's where your spike comes from.
2048 * 1536 = 3145728 pixels. At 4 bytes per pixel that is 12 MB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With