ImageIO_PNG_DATA keeps on growing after application is moved from background to foreground

2k views Asked by At

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

There are 1 answers

2
Reinhard Männer On

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