如何在.net中配置一个班? [英] How to dispose a class in .net?

查看:168
本文介绍了如何在.net中配置一个班?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

立即回了.NET垃圾回收器最终会释放内存,但是如果你想要的记忆?什么code你需要在一个类来使用 MyClass的调用

The .net garbage collector will eventually free up memory, but what if you want that memory back immediately? What code do you need to use in a class myclass to call

myclass.dispose

和释放所有在变量和对象的 MyClass的使用空间

and free up all the used space by variables and objects in myclass?

推荐答案

IDisposable接口无关,与释放内存。 IDisposable的是释放的非托管的资源的模式 - 和记忆是非常肯定受管资源

IDisposable has nothing to do with freeing memory. IDisposable is a pattern for freeing unmanaged resources -- and memory is quite definitely a managed resource.

指向GC.Collect的(该链接)是正确的答案,但使用这个功能一般是不鼓励由Microsoft .NET文档。

The links pointing to GC.Collect() are the correct answer, though use of this function is generally discouraged by the Microsoft .NET documentation.

编辑:已经赢得了这样的回答大量的业力,我觉得有一定的责任,细说一下,免得一个初来乍到.NET资源管理得到错误的IM pression。

Having earned a substantial amount of karma for this answer, I feel a certain responsibility to elaborate on it, lest a newcomer to .NET resource management get the wrong impression.

在一个.NET过程中,有两种资源 - 托管和非托管。 管理是指运行在资源的控制权,而非托管的意思,它是程序员的责任。而真的只是一种管理资源,我们在.NET关心今天 - 记忆。程序员告知运行时分配内存之后,它完全取决于运行时要弄清楚的时候内存可以被释放。该.NET使用用于此目的的机制称为垃圾收集,你可以简单地通过谷歌找到很多的有关GC互联网上的信息。

Inside a .NET process, there are two kinds of resource -- managed and unmanaged. "Managed" means that the runtime is in control of the resource, while "unmanaged" means that it's the programmer's responsibility. And there really is only one kind of managed resource that we care about in .NET today -- memory. The programmer tells the runtime to allocate memory and after that it's up to the runtime to figure out when the memory can freed. The mechanism that .NET uses for this purpose is called garbage collection and you can find plenty of information about GC on the internet simply by using Google.

有关的其他各种资源,.NET不知道清洁起来所以它必须依靠程序员做正确的事情什么。为此,该平台为程序员提供了三个工具:

For the other kinds of resources, .NET doesn't know anything about cleaning them up so it has to rely on the programmer to do the right thing. To this end, the platform gives the programmer three tools:

  1. IDisposable接口,并在VB和C#
  2. 在使用的声明
  3. 终结
  4. 在被许多BCL类实现IDisposable模式

其中第一项允许程序员高效地获取资源,使用它,然后在同一方法中释放了这一切。

The first of these allows the programmer to efficiently acquire a resource, use it and then release it all within the same method.

using (DisposableObject tmp = DisposableObject.AcquireResource()) {
    // Do something with tmp
}
// At this point, tmp.Dispose() will automatically have been called
// BUT, tmp may still a perfectly valid object that still takes up memory

如果AcquireResource是,(例如)打开一个文件和处置工厂方法自动关闭文件,那么这个code不能泄露的文件资源。但对于TMP对象本身的存储器很可能仍然被分配。这是因为IDisposable接口有垃圾收集器绝对没有任何关系。如果你的没有的要确保内存被释放了,你唯一的选择是调用 GC.Collect的()来强制垃圾收集。

If "AcquireResource" is a factory method that (for instance) opens a file and "Dispose" automatically closes the file, then this code cannot leak a file resource. But the memory for the "tmp" object itself may well still be allocated. That's because the IDisposable interface has absolutely no connection to the garbage collector. If you did want to ensure that the memory was freed, your only option would be to call GC.Collect() to force a garbage collection.

不过,不能强调不够,这可能不是一个好主意。它通常要好得多,让垃圾收集器做什么,它的目的是做什么,这是管理内存。

However, it cannot be stressed enough that this is probably not a good idea. It's generally much better to let the garbage collector do what it was designed to do, which is to manage memory.

如果所述资源被用于在较长的时间周期,它的寿命跨过几个方法会发生什么情况,这样?显然,使用的声明不再适用,因此,程序员必须手动调用处置时,他或她与资源完成。如果程序员忘记发生了什么?如果没有回退,则该进程或计算机最终可能会耗尽任何资源不被释放正确的。

What happens if the resource is being used for a longer period of time, such that its lifespan crosses several methods? Clearly, the "using" statement is no longer applicable, so the programmer would have to manually call "Dispose" when he or she is done with the resource. And what happens if the programmer forgets? If there's no fallback, then the process or computer may eventually run out of whichever resource isn't being properly freed.

这就是终结进来一个终结是在你的类中的方法,有与垃圾回收器的特殊关系。 GC的承诺, - 释放内存的该类型的任何对象之前 - 它会先给终结有机会做一些清理

That's where finalizers come in. A finalizer is a method on your class that has a special relationship with the garbage collector. The GC promises that -- before freeing the memory for any object of that type -- it will first give the finalizer a chance to do some kind of cleanup.

因此​​,在一个文件中的情况下,我们在理论上不需要关闭文件手动的。我们只是等待,直到垃圾收集器获取到它,然后让​​终结做的工作。不幸的是,这并不在实践中很好地工作,因为垃圾收集器运行的非确定性。该文件可以保持打开相当长的时间比程序员预期。并且如果有足够的文件被保持打开,该系统可以试图打开一个附加文件时失败。

So in the case of a file, we theoretically don't need to close the file manually at all. We can just wait until the garbage collector gets to it and then let the finalizer do the work. Unfortunately, this doesn't work well in practice because the garbage collector runs non-deterministically. The file may stay open considerably longer than the programmer expects. And if enough files are kept open, the system may fail when trying to open an additional file.

对于大多数的资源,我们希望这两个东西。我们希望有一个约定,以便能够说:我们正在与该资源现在做的,我们要确保有至少有一些机会清理自动发生,如果我们忘了做手工。这也正是IDisposable的模式发挥了作用。这是一个约定,允许IDispose和终结,以很好地一起玩。你可以看到该模式的工作原理是在看为IDisposable的的官方文档。

For most resources, we want both of these things. We want a convention to be able to say "we're done with this resource now" and we want to make sure that there's at least some chance for the cleanup to happen automatically if we forget to do it manually. That's where the "IDisposable" pattern comes into play. This is a convention that allows IDispose and a finalizer to play nicely together. You can see how the pattern works by looking at the official documentation for IDisposable.

底线::如果你真的想要做的是,只要确保内存被释放,那么IDisposable接口和终结不会帮你。但IDisposable接口是所有.NET程序员应该明白一个极其重要的模式的一部分。

Bottom line: If what you really want to do is to just make sure that memory is freed, then IDisposable and finalizers will not help you. But the IDisposable interface is part of an extremely important pattern that all .NET programmers should understand.

这篇关于如何在.net中配置一个班?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆