1

I have one UIImageView that I use to display different images at different points in time. When I want to change the image displayed I use the following code (where filenamePng holds the image name to be loaded and imgView is my UIImageView):

    [self.imgView setImage:[UIImage imageNamed:filenamePng]];

When I look at the 'Real Memory Usage' of my app in Instruments, as different images are displayed, the memory just continues to increase (some of the png's are quite large so its easy to tell), I have played around with setting the UIImageView to nil/releasing the it but to no avail.

Can someone give me an indication as to why when using the above code it appears that previous images in memory are not released even when set to nil/released?

MattStacey
  • 875
  • 4
  • 12
  • 23
  • Do you use ARC? Do you reference the images somewhere else where they might be retained? – sch Feb 20 '12 at 23:24
  • The code you posted has no leaks. Besides, if you are not calling `alloc` you are not supposed to call `release`. If you are calling `alloc` then post the code with the relative `release` – Manlio Feb 20 '12 at 23:26

1 Answers1

1

+[UIImage imageNamed:] uses a cache. Assuming all your ref counting is correct, it should purge at some point. Also, the execution on sim vs device can be much different in this regard.

justin
  • 104,054
  • 14
  • 179
  • 226
  • 1
    thanks Justin - your answer led me to this question: http://stackoverflow.com/questions/316236/vs-uiimage-imagewithdata – MattStacey Feb 20 '12 at 23:36
  • 1
    and now this one http://stackoverflow.com/questions/924740/dispelling-the-uiimage-imagenamed-fud – MattStacey Feb 20 '12 at 23:39