复活对象的用法 [英] Usages of object resurrection

查看:108
本文介绍了复活对象的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET Windows服务应用程序中的内存泄漏有问题.因此,我开始阅读有关.NET中内存管理的文章.而且我在 Jeffrey Richter的一篇文章中找到了一种有趣的做法.该练习名称是对象复活".看起来好像是将全局变量或静态变量初始化为"this"的代码:

I have a problem with memory leaks in my .NET Windows service application. So I've started to read articles about memory management in .NET. And i have found an interesting practice in one of Jeffrey Richter articles. This practice name is "object resurrection". It looks like situating code that initializes global or static variable to "this":

protected override void Finalize() {
     Application.ObjHolder = this;
     GC.ReRegisterForFinalize(this);
}

我知道这是一个不好的做法,但是我想知道使用这种做法的模式.如果您知道的话,请在这里写.

I understand that this is a bad practice, however i would like to know patterns that uses this practice. If you know any, please write here.

推荐答案

推测性:在Pool情况下,例如ConnectionPool.

Speculative: In a Pool situation, like the ConnectionPool.

您可以使用它来回收未正确处理但应用程序代码不再保留引用的对象.您不能将它们保留在池"中的列表"中,因为那样会阻止GC收集.

You might use it to reclaim objects that were not properly disposed but to which the application code no longer holds a reference. You can't keep them in a List in the Pool because that would block GC collection.

这篇关于复活对象的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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