I am trying to understand why I have memory leaks in a very basic implementation of a UIImage and a UIImageView.
I am not using ARC in that case (correctly disabled).
My code is pretty straightforward :
UIImage *image = [UIImage imageNamed:@"my_image.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[[self view] addSubview:imageView];
[imageView release];
I am implementing this code in the viewDidLoad:
method of a UIViewController.
By calling the method imageNamed:
of UIImage, I know that I will get an object that I do not own / an autorelease object. This object will also be retained by the UIImageView object instantiated just after. So the only object I have the ownership is the UIImageView one.
After running this app with the Memory Leaks Instruments, I have this report :
I heard about the cache system that operates but I should not have have memory leaks because some datas are cached.
Here is a reference to the answer with the cache explanation : https://stackoverflow.com/a/2930567/1154501
Thank you in advance !
Edit : Tried also with ARC, and I got the same issue.
[UIImage imageNamed:]
is managed by the operating system. Deallocating the UIImage created from this method might free up the memory that was allocated. If you have lots of images or user generated content, you should be using [UIImage imageWithContentsOfFile:]
or [UIImage imageWithData:]
.
If you create too many images with [UIImage imageNamed:], your app may get killed by iOS because of memory usage. I made a sample app to prove this to myself, see more here: iOS UIImage storage formats, memory usage and encoding / decoding
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With