Am I supposed do dispose the Image property of a UIImageView to help Garbage Collector in Monotouch?

garbage-collection, ios, uikit, xamarin.ios

Solution

am I supposed to Dispose() the Image property

Quick answer: No.

Long answer:

The `UIImageView.Image` property, `image` selector, is retained. Even if you dispose it (from the managed side) the native retainCount (Objective-C is reference counted) of the `UIImage` will still be higher than 0 and the (native side of the) `UIImage` won't be freed.

When you dispose the `UIImageView` it will release the `UIImage` it use (Apple's Objective-C code) and, if the retainCount reach 0, it will be natively freed.

If there's a large delay between when you stop needing the `UIImage` and the time you dispose of the `UIImageView` then you might want to set `UIImageView.Image` to `null`. That will release (in the ObjC way, i.e. drop its retainCount by one) the `UIImage` and, if it's not used elsewhere, it will be (natively) freed.

Note that, if the `UIImageView` is not disposed, then setting its `Image` property to another `UIImage` will have the same result (the old one's will be released).

Problem

On my quest to reduce memory usage, another question. I see that `UIImage` and `CGImage` might be candidates for high memory usage in my app. Wherever I use a UIImage, I try to wrap it in a using block to have it `Dispose()`'d as soon as possible. However, often the `UIImage` ends up in a `UIImageView.Image` property. If I remove the `UIIImageView` from its Superview, am I supposed to `Dispose()` the `Image` property before and set it to null or is this wasted typing?

Original source