2

I have an application in which i am using lots of images but i have found an abnormal issue with the application memory footprints. I am using imageNamed method to initialise UIImage objects. From the documentation i have read that imageNamed keeps the memory in cache and does not reload the images every time. This works for me because my application is based on images only but in my case it seems that each time my application is moved from background to foreground the images are loaded again. This i feel because when i did memory profiling for the application in each generation whenever i moved from background to foreground the amount of memory consumed increased tremendously in VM:ImageIO_PNG_DATA and ultimately my application crashed as it was consuming more than 600 MB. At some places i read that we should use imageWithContentsOfFile to avoid this issue but I am not sure that whether this is a right approach or not.

Please guide me regarding the same.

1 Answers1

4

The docs say: "If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app."
So, if your images are not re-used, you should use imageWithContentsOfFile: instead of imageNamed:.

Another point is that both methods create autorelease objects. Even if these objects are no longer used, they are deleted from memory only when the autorelease pool is drained. If you did not set up your own autorelease pool, it might be drained rarely. You would possibly use less memory, if you set up your own autorelease pool using a block like

@autoreleasepool {
    // Your code here
} // @autoreleasepool
Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
  • Thanks for the quick reply Reinhard but my images are the ones that are used on many views so i want them to remain in cache so that they are not loaded from disk every time. Therefore imageNamed suits my requirements. Also I am not sure how i would use @autoreleasepool for all the images which i am assigning to my UI components. Please guide me on this. – Vishal Dwivedi May 26 '14 at 12:19
  • If you re-use your images often, then setting up an autorelease pool won't help. It is useful only if autorelease objects are instantiated for short-time usage. You can find some info here: – Reinhard Männer May 26 '14 at 12:40