活动和内存泄漏在.NET [英] Events and Memory Leaks in .NET

查看:133
本文介绍了活动和内存泄漏在.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#.net 3.5 ... ...我一直在努力脱钩移动的数据库相关的活动成为一个单独的工人对象BLL对象。工人对象添加实体到数据库和事件的成功或失败消息发送回一个BLL对象。

I'm using C# .NET 3.5 ... and I've been working to decouple a BLL object by moving database related activity into a seperate worker object. The worker object adds entities to the database and events a success or failure message back to a BLL object.

当我实例在BLL我电汇了工人的事件,并使用事件+ =委托(事件处理程序)的语法设置BLL的事件处理程序中的工人对象。

When I instance the worker object in the BLL I wire up the worker's events and set the BLL's event handler using the event += delegate(eventhandler) syntax.

我听说,如果我没有明确的无线化与听众的 - 。=语法时,工作人员布置有内存泄漏的潜在

I've heard that if I don't explicitly unwire the listeners with the -= syntax when the worker is disposed that there is a potential for memory leaks.

在一个窗口服务拾取消息从队列,并调用相应的BLL对象,所有这一切都处理的时...我担心我可能会引入内存泄漏到这个过程中。

All of this processing occurs in a windows service that picks up messages from a queue and calls the appropriate BLL object ... I'm worried that I might be introduce a memory leak into this process.

推荐答案

订阅事件增加了从用户到提供一个参考。

Subscribing to an event adds a reference from the subscriber to the provider.

x.Event + = y.handler意味着x现在拥有一个引用到y

x.Event += y.handler means that x now holds a reference to y

如果x具有更长的寿命比y,则Y不能被垃圾收集,直到到x的所有引用都消失了。

If x has a longer lifespan than y then y cannot be garbage collected until all references to x have disappeared.

在你的情况,你正在听的工人血铅事件(如果我理解正确的话),那么你必须要引用中剩余BLL,除非你明确地取消了工人。

In your case you are listening to events from the workers in the BLL (if I understand you correctly) then you have references to the workers remaining in the BLL unless you explicity unsubscribe.

不过,如果你是在完成与工人同时作为BLL那么它不会真的无所谓。

However, if you are finishing with the worker at the same time as the BLL then it won't actually matter.

这篇关于活动和内存泄漏在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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