ASP.NET 清理非托管资源的最佳方法

发布于 2025-01-05 07:59:00 字数 533 浏览 1 评论 0原文

我对 ASP.NET 应用程序中的内存清理有点困惑。我定义了几个析构函数——我知道这不是新的 .NET 做事方式,但我是一个习惯性的人,而且我总是在 C++ 中这样做——它们在几乎所有场景中都运行得很好。但是,我注意到有时在我的 ASP.NET 应用程序中不会调用它们。

我正在考虑实现 IDisposable,但我的印象是 IDisposable 是供代码的其他用户使用的,并且我不确定 ASP.NET 在完成该对象时是否会调用 Dispose。有人可以澄清一下吗?

什么是最好的,我的意思是它总是有效的清理我的非托管内存的方法?

编辑

似乎表明,如果包含潜在非托管内存的类是封装类的成员,那么析构函数是最佳策略。这对我来说当然是有意义的,因为我几乎无法在班级成员周围进行尝试或使用。然而,即便如此,这又让我回到了我的问题,它有时永远不会在我的 ASP.NET 应用程序中被调用。

I am a little confused about memory clean up in an ASP.NET application. I had defined several destructors--I know this isn't the new .NET way of doing things, but I am a creature of habit and I always did it this way in c++-- that were working wonderfully in just about every scenario. However, I have noticed that they are sometimes not called in my ASP.NET applications.

I am thinking about implementing IDisposable, but I am under the impression that IDisposable is for other users of your code, and I am not sure that ASP.NET would call Dispose when it is finished with the object. Could someone clarify on this?

What is the best, and by best I mean that it will always work-- way to clean up my unmanaged memory?

Edit

This seems to indicate that if the class containing potential unmanaged memory is a member of an encapsulating class, then the destructor is the best strategy. This certainly makes sense to me since I could hardly put a try or a using around a class member. Even then however, that brings me back to my question, it sometimes never gets called in my ASP.NET app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2025-01-12 07:59:00

所有处理非托管资源的类都应实现 IDisposable界面。

有关更多信息,垃圾收集器存在两个问题。首先,你不知道它什么时候会运行。其次,它对非托管资源的了解为零。这就是它们被称为非托管的原因。

因此,由调用代码来正确处置利用非托管资源的对象。最好的方法是实现上述接口并将对象包装在 using ( ) { } 语句,或者至少是一个 try ..finally。我通常更喜欢 using 语句。

此外,通过实现 IDisposable,您可以向其他开发人员发出信号,表明此类处理非托管资源,以便他们可以采取适当的步骤来确保正确调用事物。

All classes which handle unmanaged resources should implement the IDisposable interface.

For a little more info, there are two issues with the garbage collector. First, you have no idea when it's going to run. Second, it has zero knowledge of unmanaged resources.. That's why they are called unmanaged.

Therefore it's up to the calling code to properly dispose of objects that utilize unmanaged resources. The best way to do this is to implement the above interface and either wrap the object in a using ( ) { } statement or, at the least, a try .. finally. I generally prefer the using statement.

Also, by implementing IDisposable you are signaling to other developers that this class deals with unmanaged resources so they can take the appropriate steps to ensure things are called correctly.

傾城如夢未必闌珊 2025-01-12 07:59:00

使用托管资源时,您不需要实现 IDisposable 或析构函数。对于“清理”,您所要做的就是将所有顶级(“根”)引用设置为 null(静态通常被认为是顶级),垃圾收集器将处理其余的事情。

此类析构函数主要在调用者忘记调用 Dispose 或无法进行此类调用的情况下对非托管资源有用。然而,运行时并不保证析构函数会被调用;只是它们将在与对象关联的内存最终释放之前被调用。您不必实现IDisposable;这只是一个惯例。使用 Close() 或 Cleanup() 方法来释放非托管资源是完全合理的。

When working with managed resources, you don't need to implement IDisposable or a destructor. All you have to do for "cleanup" is set all top-level ("rooted") references to null (statics are normally considered to be top-level), and the garbage collector will take care of the rest.

Destructors as such are primarily useful with unmanaged resources in cases where callers either forget to call Dispose, or where such a call isn't possible. However, the runtime doesn't guarantee that destructors will ever be called; only that they will be called before the memory associated with the object is finally freed. You don't have to implement IDisposable; it's just a convention. It's perfectly reasonable to have a Close() or Cleanup() method that releases unmanaged resources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文