Wednesday, April 24, 2013

No memory leak, still app is out of memory?

In iPhone/iPad app I have came across many situations where there was absolutely no memory leak, but app was crashing after some time due to out of memory. The reason could be many. Your application may be huge and you are using many resources/data which you are not using but still not releasing them.

Most of the time this happens with images. Below line could be fetal.


UIImage *img = [UIImage imageNamed:@"someimage.png"];

The method imageNamed from UIImage caches image data so that it could load it faster next time. This is great for smaller images, but shouldn't be used for heavy images. Instead use imageWithContentsOfFile method.

What happens with imageNamed method is that, if your app loads n number of images then all those images will be cached and will never be released until the app is quit. You may not see any memory leak using static-analyzer or in instruments. If you see the allocation in instruments you may see steady increase in the allocated memory which is never used.