Do I win memory by explicitly disposing imageView.Image?

戏子无情 提交于 2019-12-22 17:35:24

问题


I have this code in my app:

var newImage = // ...

if (imageView.Image != null && imageView.Image != newImage)
    imageView.Image.Dispose ();

imageView.Image = newImage;

I have three related questions:

  • Does it immediately release the memory occupied by the previous imageView.Image?
  • If it does, is there a cleaner solution?
  • Does this have anything to do with NSAutoreleasePool?

回答1:


Does it immediately release the memory occupied by the previous imageView.Image?

Not immediately but it should be much faster than waiting for the Garbage Collector.

Calling Dispose will drop the managed reference to the native UIImage. If nothing else (natively) has a reference to the UIImage (RetainCount == 0) then it will be freed (ObjC reference counting).

In your code imageView still has a reference to it until its Image property is set to newImage - which is why I answered not immediately.

If it does, is there a cleaner solution?

Not really. Letting the GC do it's work is cleaner looking - but image can be very big and are worth freeing asap.

Also it's not really worth (and would not be cleaner anyway) adding a local variable to make sure (if no other native reference exists) the image will be freed immediately - it will happen on the next line.

Does this have anything to do with NSAutoreleasePool?

Anything ? well it's memory related in both cases.

Creating images will use (cache) the current NSAutoreleasePool and will, eventually, be drained. If you process a lot of stuff (e.g. a loop) then it's often worth having your own, short-lived, pool to ensure a faster drain.

Some API (well know to require a lot of memory) are decorated with an attribute that will automatically add (btouch) an NSAutoreleasePool - but it's not easy to find out which.

In doubt you better use Apple Instruments to measure...



来源:https://stackoverflow.com/questions/16202692/do-i-win-memory-by-explicitly-disposing-imageview-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!