When I use UIImage imagenamed: should I set the variables which hold UIImages to nil before exit? I notice that sometimes when I switch between views which have UIImages, the memory keeps growing and growing with each switch back an开发者_StackOverflow中文版d forth.
Setting variables to nil is not necessary.
Setting properties to nil (self.property = nil;
) will release them if they're declared @property (retain)
.
Since +imageNamed:
doesn't start with "alloc", "copy", "new", or "retain", you don't have to release it. It's possible things are staying in memory because the space isn't needed. Are you seeing any leaks or just memory usage?
Setting UIImage variables to nil
won't do anything particularly useful. Also, you shouldn't release the image returned from +imageNamed:
, since the method name does not imply that you have ownership of the returned object.
Cocoa maintains an image cache. Subsequent calls to imageNamed:
will return the same UIImage
object if it is loaded already (since UIImage
objects are immutable), otherwise it will load it again into the cache. The lifetime of the image in the cache is up to Cocoa to decide. In low-memory situations the image data may be purged. Even though the actual internal image data is purged from the cache, the object that you have is still able to reference the image (Cocoa will reload the image data if it was purged from the cache). It is explained throughout the UIImage
documentation.
If your memory usage is consistently growing then the leak is probably coming from somewhere else.
精彩评论