Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading full-screen Retina image on iPad causes massive 10 MB spike

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: output from Instruments

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?

like image 436
jowie Avatar asked Dec 01 '25 18:12

jowie


1 Answers

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.

like image 161
Filip Radelic Avatar answered Dec 06 '25 04:12

Filip Radelic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!