关闭 winform 应用程序时将对象设置为 null 通常是一个好主意吗?
在 C# 2.0 winform 应用程序中将托管对象设置为 null 通常是一个好主意,即在关闭窗体和控件时将其设置为 null,还是应该将其留给垃圾回收。
我的项目中的类之一称为 Job,它存储 String 和 Lists 类型等。是否有必要这样做:
if (Job != null)
{
Job = null;
}
或者这仅对于非托管资源(例如具有 dispose 方法的文件句柄)是必需的。我绝对应该调用它然后设置为空。
任何澄清都会很好。谢谢。
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 classes 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不 - 当你关闭应用程序时,你应该做任何你需要的事情来刷新文件句柄之类的东西(否则你可能会丢失数据),但你不需要担心内存。您的进程即将停止运行 - 此后它不可能占用任何内存,除非出现操作系统错误!
即使您的进程不会即将终止,您通常也不应该将变量设置为 null。在几乎所有情况下,垃圾收集器都会执行您想要的操作。如果您知道变量本身仍然是“活动的”,但您不希望该变量当前引用的对象再保持活动状态,则只需将变量设置为 null 即可。这是非常罕见的。
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...
你应该把它留给垃圾收集器。
是的,只有非托管资源才需要设置为 null(或其他手动垃圾处理方法)。
You should leave that to garbage collector.
And yes, setting to null (or other manual garbage disposal methods) are only necessary for unmanaged resources.
不,在 C# 中没有必要。此规则来自 C/C++,其中在 free/delete 调用后将变量设置为 NULL 是个好主意:
在这种情况下,您始终知道如果
foo != NULL
那么该对象仍然存在,并且相反,如果 foo == NULL 则该对象肯定已死亡。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:
In this case you always know that if
foo != NULL
then the object is still alive, and conversely iffoo == NULL
then the object is definitely dead.以下是如何处置的示例
Here is example how to Dispose
每当您单击用它编译的程序时,桌面应用程序的.Net框架就会实例化自己,它的作用是获取一个进程空间并在其中加载您的程序,这样它就可以管理它承诺提供的所有内容,例如垃圾收集。因此,托管资源的处置不是强制性的,因为它会在从内存中卸载自身之前自动进行处置,但对于非托管资源,您必须自己处置它们,否则 .net 不承诺处置它们。
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.
不需要,在关闭应用程序之前无需将引用设置为 null,无论是托管对象还是处置后的一次性对象。
垃圾收集器只关心引用是否可以使用。当您退出表单类时,其中的每个成员都将变得无法访问并且可以被收集,因此将任何引用设置为 null 只是浪费时间。
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.