MonoTouch/MT Garbage Collector: how to correctly release my views/class members (simple example)?

后端 未结 2 1092
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 17:19

Have a look at the sample below which is not completely implemented but demonstrates what I\'m talking about:

public class MyClass : UIView
{
  private UIVie         


        
相关标签:
2条回答
  • 2021-01-13 17:30

    I would just reuse your SubView instead of making a new one each time. So create it in ViewDidLoad, store it in a member variable, and reuse it.

    I don't think the extra memory will be a problem, and you won't have to worry about the GC at all.

    0 讨论(0)
  • 2021-01-13 17:37

    The answer is yes, the garbage collector will now consider the two previous instances of the objects to not be reachable from anywhere, and will be collected the next time the GC runs.

    You do not need to call Dispose() manually. The only reason to call Dispose() manually is in cases where you know that the object you are pointing to is some large object and you want to make sure that the memory is released immediately, and not wait for the GC to kick-in at some point in the future.

    What happens when you call Dispose () on the object, is that we internally release the reference to the Objective-C object and we set the Handle property on the object to null. This means that if you try to call another method on a disposed object, you will get a nice exception.

    0 讨论(0)
提交回复
热议问题