开发者

Is it generally a good idea to set objects to null when closing winform application?

Is it generally a good idea to set managed objects to null in C# 2.0 winform application, i.e. within forms and controls when closing them down, or should I leave that to garbage collections.

One of the classe开发者_运维百科s in my project is called Job, that stores String and Lists types etc. Is it necessary to do:

if (Job != null)
{
    Job = null;
}

Or is this only necessary for unmanaged resources, e.g. file handles that have a dispose method. I should definitely call that and then set to null.

Any clarification would be great. Thank you.


No - when you're shutting down the application you should do anything you need to flush things like file handles (or you might lose data) but you don't need to worry about memory. Your process is about to go down - it can't possibly take any memory after that, barring an OS bug!

Even when your process isn't about to terminate, you generally shouldn't be setting variables to null. In almost all cases, the garbage collector will do what you want anyway. You only need to set a variable to null if you know that the variable itself will still be "live" but you don't want what the object to which it's currently referring to be kept alive by that variable any more. This is pretty rare.


No, it's useless. If your application is going to be stopped, the memory, files handles and so on will be freed anyway when the process terminates...


You should leave that to garbage collector.
And yes, setting to null (or other manual garbage disposal methods) are only necessary for unmanaged resources.


No. It is not necessary in C#. This rule came from C/C++ where it is good idea to set variable to NULL after free/delete call:

T *foo = new T();

// ...

if (foo) {
    delete foo;
    foo = NULL;
}

In this case you always know that if foo != NULL then the object is still alive, and conversely if foo == NULL then the object is definitely dead.


Here is example how to Dispose


The .Net framework for desktop application instantiates itself whenever you click your program compiled with it, what it does is it acquires a process space and Load your program in it, this way it can manage all the things that it promises to offer e.g. garbage collection. SO disposing of managed resources is not mandatory since it does so automatically before unloading itself from memory but for UNMANAGED Resources you must dispose them off by yourself, otherwise .net does not promises to dispose them.


No, there is no need to set references to null before closing the application, neither managed objects, nor disposable objects after you have disposed them.

The garbage collector only cares about whether the references can be used or not. When you exit your form class, every member in it becomes unreachable and can be collected, so setting any references to null is just a waste of time.


No, it's not necessary, there is the Garbage Collector that do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜