2
votes

Still pain with memory debugging. I have 4 VC's I load by using navigation controller. Every VC has its own PNG images used for several controls. In Instruments I realized that most of the VM regions are occupied by ImageIO_PNG_Data. And as I push/pop VC's those VM increases and never decrease (I was supposing that dealloc some VC would also release images). enter image description here

Of course, the debug is done in the Simulator.

2
Image data is cached in separate cache system that is not directly affected with dealloc. UIImageViews are deallocated at that time, for example - but images can remain in cache "just in case" you would need them again. They should be deallocated when your app receives a memory warning though. - Rok Jarc
So, what does mean the number shown in Debug Navigator of XCode 5, heap or total? Zaph, could u make an answer? - gdm
I don't know. I use instruments allocations and HeapShot analysis to determine how I am using memory. For HowTo use Heapshot to find memory creap, see: bbum blog - zaph

2 Answers

6
votes

To expand slightly on rokjarc's comment:

UIImage +imageNamed: explicitly caches. The documentation states:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

So images loaded previously will remain in the cache unless or until the memory is needed elsewhere. There's no efficiency to be gained from freeing memory up needlessly.

If you want to avoid the caching for whatever reason — I would argue whatever spurious reason — you could use +imageWithContentsOfFile:, or the normal init equivalent, having obtained the full path from NSBundle.

PNGs set to image views and other places via the interface builder will be accessed via the cache as far as I'm aware.

2
votes

If the VM allocations do not have physical memory allocated to them there is no problem.

iOS memory maps files and there may be no physical memory allocated at any given time. Some VM allocations are frameworks that are shared by other apps.

What you need to watch are Living Heap Allocations which in this case is a little over 4MB.