1

So apparently, my app crashes on ipod 2nd generation due to low memory issue. What I do was calling image on each view within scrollView + pageControl when user scrolls. And app crashed when it reached a particular point after got memory warning. I tried to free up view when I got warning but it still caused crash.

I googled about ImageNamed: and apparently there was issue within this api call, but most article said it was fixed in recent iOS version.

I fixed this problem with calling image imageWithContentOfFile instead imageNamed, but I'm wondering if ImageNamed still causes memory leak or not free up when it view is released.

REALFREE
  • 4,378
  • 7
  • 40
  • 73
  • What OS version is running on your iPod Touch 2g? – tc. Jun 28 '11 at 00:52
  • it's not leaking the image, it's caching it (as explained below by RyanR), only use `imageNamed:` for small images that will always be used by your app and in multiple locations – bshirley Jun 28 '11 at 02:24

1 Answers1

7

imageNamed: doesn't cause a leak, but it is frequently misunderstood which is what leads to memory issues when it's used. It caches the uncompressed image after it is loaded, which means there are immediately 2 copies of that image in memory. If you use it for small, frequently used images (such as icons), this is great because the runtime doesn't have to fetch the file off disk - it's already available in the cache. Where this gets users into trouble is when they use imageNamed: to load a large image, say a 4MP image taken with a camera. That image takes up quite a bit of memory: 4 million pixels, types 4 bytes per pixel = 16MB of memory, TWICE. If you use that method to load images for your slideshow, photo sharing, camera app, or whatever, it adds up real fast.

So if those features don't fit what you need, use one of the other UIImage loading methods. You users will thank you.

Note: This information comes from the Apple Engineer which presented the UIKit rendering session (#121 I think it was). Hopefully my notes are correct :)

RyanR
  • 7,728
  • 1
  • 25
  • 39
  • I understand imageNamed cache image file, but my image is pretty small (40kb to 500kb) and still ended up getting trouble with memory on ipod 2nd generation.. probably 2nd generation has pretty small amount of memory ? (works fine on iphone4) – REALFREE Jun 28 '11 at 02:43
  • Might be that. Also, 500kb might be the _compressed_ size of your image, but the size in memory will be considerably more: Length x Width x Bits per pixel. Did you try running it in Instruments? – RyanR Jun 28 '11 at 02:49
  • yea, I checked with instruments and I didn't see that much of allocation occurred. It was about 1-2MB allocation total – REALFREE Jun 28 '11 at 07:50
  • @REALFREE that's because the allocation instrument is incorrect. Follow this: http://stackoverflow.com/questions/2865786/low-memory-with-640kb-of-live-bytes btw 500kb image IS large for a mobile device. – polyclick Dec 11 '11 at 23:37