3

I'm having trouble with UIImage memory management.
(iphone) my app gets memory warning when instruments shows "all allocations" only 7mb?

Essentially, I create/destroy UIImage many times.
Even if I make memory consumption at a time low using local NSAutoreleasePool, iphone/ipad device keeps complaining about memory usage.

I'm thinking, I should allocate UIImage to specific memory buffer(or pool of buffer) which I can designate to.
Is this possible?

Or any other suggestion on how to create/destroy UIImages many times without upsetting iOS would be much appreciated.

Thank you.

Community
  • 1
  • 1
eugene
  • 39,839
  • 68
  • 255
  • 489

2 Answers2

10

It is not a well known fact, but Instruments does not consider the size of UIImages within the allocated memory. You could be leaking 100Mb of UIImages and the allocated memory would not tell the story. What does tell the story is the number of live UIImage instances at a given moment.

In the Allocations instrument, using the top-right search box, search for "Image". Then look for the column labeled #linving in UIImage. That number is the number of UIImage objects. That is a better compass in to figuring out where are your UIImages going. If at some point you have more objects than what you expect, and thease UImages are not going away you have a UIImage leak in your hands. An UIImage leak will kill your app real fast.

fsaint
  • 8,759
  • 3
  • 36
  • 48
  • wow nice info, but unfortunately, My #living UIImage doesn't go over my expectation. :( any other suggestion? , and all others including #CGImage doesn't fluctuate more than I think(max 6+).. umm maybe 6+ = 6mg and baseline 10mg = 16Mg triggered the warning. – eugene Jan 10 '11 at 18:31
  • I have fresh clean ipad with less-than-4.0 iOS (with no background app capability). Even though people said 16MG is enough to generate a warning, 16mg still seems too low to me. I'll have to make sure it's not memory-fragmentation issue by lowering baseline memory usage a bit more. – eugene Jan 10 '11 at 18:36
  • Warnings aren't necessarily a cause for concern - they can and are produced fairly regularly by Apple's own applications (particularly the Maps app on retina display devices). It's what your app does in response to a memory warning that's important, not whether one is triggered. – lxt Jan 10 '11 at 18:47
  • ah, well, it dies soon after, since i'm not handling the warning. What I'm worried about is that, My device isn't running anything besides my test application. and it receives warning at 16MB. This device(ipad) is test purpose only, and I even don't have many other apps installed. – eugene Jan 10 '11 at 18:54
  • 1
    ah, in one of my UIImage create code, I found I had 2 UIGraphicsBeginImageContext() but one UIGraphicsEndImageContext(), and it must have been causing the problem. (which were not detected by Instruments though) Thanks all – eugene Jan 12 '11 at 04:02
  • Interesting. Glad you found the problem. Cheers! – fsaint Jan 12 '11 at 08:36
2

CGImage doesn't fluctuate more than I think(max 6+).. umm maybe 6+ = 6mg and baseline 10mg = 16Mg triggered the warning

CGImages are not autoreleased, you should call CGImageRelease(image);

Heisenbug
  • 951
  • 6
  • 25