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

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

问题描述

我的 .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.

推荐答案

Speculative:在 Pool 情况下,如 ConnectionPool.

Speculative: In a Pool situation, like the ConnectionPool.

您可以使用它来回收未正确处置但应用程序代码不再持有其引用的对象.您不能将它们保存在 Pool 中的 List 中,因为这会阻止 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天全站免登陆