Yesterday evening, I wrote a very simple prototype ipad app to try out some stuf. Basically I’m just displaying some images fullscreen and swiping left/right gives you the next or previous picture. Plenty has been written about the memory/caching difference between using [UIImage imageNames] vs [UIImage imageWithContentsOfFile] (see this and this and many more), but I was flabbergasted with the amount of memory my very simple app would eat just for displaying four (yes, 4!) images I had (click to enlarge).
21 MB dirty and 69 MB resident. Just for displaying 4 images that are about 300K each consecutively full screen. You can clearly see with each new image the memory goes up. I did some googling around, and quickly learned that the following line caused the huge memory footprint:
UIImage *currentImage = [UIImage imageNamed:[self.photos objectAtIndex:self.currentIndex]];
Apparently imageNamed caches images to faster display the next times you need them. I also found older iOS versions (2 and 3) leaked memory when using this method, but I believe this should be fixed in current version. For small icons and layout images, I can completely understand why you want to cache the images so aggressively. However, in this use case my image gallery could have potentially hundreds of full-screen images. I haven’t found any way to configure the size of the image cache.
It just doesn’t feel right to use that much memory. So I switched to use following code:
NSString *file = [[NSBundle mainBundle] pathForResource:[self.photos objectAtIndex:self.currentIndex] ofType:nil]; UIImage *currentImage = [UIImage imageWithContentsOfFile:file];
Which gives following memory usage
Memory is now down to around 7MB dirty size, but more importantly it stays constant! Performance-wise, I cannot see any difference in loading speed between the two versions.
So for me that’s pretty clear which approach I should take for this app. Even if the image cache is automatically cleared by iOS on low memory warnings, having a more tighter control on memory gives me that warm fuzzy feeling we all like.