Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

iPhone hangs when loading 100+ images in memory

In my app I have about 100+ images that I need to load in memory. The size of each image is between 8K to 16K. But when I try to load them using [UIImage imageNamed:@"imagename.png"] and then add into an array iPhone hangs after it has loaded 50+ images and then restarts automatically.

I also do release the image after it has been added to array, and there are no memory leaks.

The size of total images is not beyond 2 MB, is it too much data that iPhone cant handle?
I also have no option to load those images lazily, so what am I doing wrong? what are my options?

Can anybody help?

Macbook pro 2.33 gz, Mac OS X (10.5.5)

Posted on Nov 18, 2008 3:13 AM

Reply
Question marked as Best reply

Posted on Nov 18, 2008 4:17 AM

There is a memory issue with the imageNamed: method. Try this instead:

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"]];


Did this help? I am also interested what what the memory limits are when it comes to images.

Message was edited by: lamouroux
12 replies

Nov 18, 2008 5:24 PM in response to Scott Squires1

The dev forum is not working for me today. Its not allowing me to post any messages, so I will continue this thread here.

I tried using instruments. Leaks and Activity Monitor.
There are no memory leaks.

And my app is not using memory beyond 4 MB.

I dont know how to check how much memory is still available on iPhone.

Still struggling !

Nov 19, 2008 10:07 AM in response to eknathkadam

I have compressed all the images, now all image sizes are between 4 to 10 KB. Activity Monitor shows that the memory taken by my app never goes beyond 4.5 MB
there are no memory leaks. But still my app crashes and before crashing gives me a memory warning.
I am still confused why so I get memory warnings even if my app doesnt eat a lot of memory or leak any memory.

Nov 19, 2008 10:25 AM in response to Stele

I actually changed the call from imageWithContentsOfFile to
[[UIImage alloc] initWithContentsOfFile:....

So that I can release it myself be sure about it. But the behavior is same. And this is not arbitrary crash, my app does receive memory warnings before it crashes.

The question is, my app is not eating a lot of memory in reality, but then why the warning.

How to find out?

Nov 19, 2008 9:49 PM in response to eknathkadam

Hi, what exactly you're measuring in instruments? In my last app i first tried to fix the same kind of problem (low memory warning, than crash) with the object allocation and the leaks instrument. But for some reason my code HAD leaks which definitely were not detected by instruments. I than started to monitor the overall memory-usage of my app and check when or where it grows.

Nov 19, 2008 9:58 PM in response to just.do.it

I am trying to measure if my app really eats a lot memory that it has to crash.
I have checked it with leaks and activity monitor. Will give it a try with object allocation as well.
I am not sure how to use Object Allocation for checking if my app is eating a lot memory.

How did you figured out you issue while instruments was failing to show the leaks?

Nov 19, 2008 10:27 PM in response to eknathkadam

Well, because of the low memory warnings someone (the iPhone or instruments) must be wrong. 🙂
So i decided to check if the leaks instrument is wrong and looked for a different instrument to check the overall memory allocation of my app.
I found the memory monitor instrument which show the memory usage for alle the processes running on the device.
I than just repeated some specific funktions of my app and checked the memory usage of my app.
In my case for example i found a leak in my implementation of

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

where i forgott to autorelease the returned view. This way i "lost" a UIView each time a new one was created for the UIPickewView. And the leaks instrument was not aware of that.

Nov 20, 2008 1:46 PM in response to eknathkadam

activity monitor should certainly show you the memory your app is using (select your app from the list below and check the memory used column.
Check the Free memory graph at the top of activity monitor. It will be going down to 2MB or less when getting memory warning.

100 images at one time is a lot.
width * height * 4 = number of bytes of memory per image. This is key. The size of the image compressed on disk has nothing to do with memory usage.
A single full frame image takes 614, 400 bytes (that's over 1/2 MB per image)
* 100 images = 61, 440, 000 bytes. (61 MB - more than you're likely to ever get)

iPhone hangs when loading 100+ images in memory

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.