垃圾收集器问题 [英] Garbage collector question

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

问题描述




假设某个对象超出范围及其引用的情况

不会保留在任何地方,而是对某个代表的引用(引用

其中一个对象的方法在一些地图中保存为一个值。


是否有可能对象被处理掉代表将

无效?


你的DoB


-

使用Opera革命性的电子邮件客户端: http://www.opera.com/ mail /

解决方案

12月11日下午12:58,DoB< D ... @ dob.comwrote:


假设某个对象超出范围及其引用的情况

不保留在任何地方,而是对某个委托的引用(引用

对象的方法之一在某些地图中保留为值。


是否有可能该对象被处理并且代表将无效?b $ b无效?



好​​吧,它可能会被处理 - 但它肯定不会是垃圾

收集。


引用一个对象'

方法之一的委托的引用与其参考不保留在任何地方的b $ b直接相矛盾。引用*是*保留在代理中 - 当然它是一个

实例方法。


Jon


对引用对象'


方法之一的委托的引用与其参考不保留在任何地方的b $ b直接相矛盾。引用*保存在委托中 - 当然,前提是它是一个

实例方法。



是的,它将是一个实例方法。


所以...

as只要保留对代理人的引用,引用的方法

可以安全地调用,是吗?


DoB


定义安全 ; -p


委托将阻止对象被垃圾收集,但如果对象本身被故意关闭/处置/无论什么(通过

认为不再需要该对象的代码)那么该方法将会在调用时抛出异常。


这种类型的委托(对于另一个未引用的对象)本身很好,但请注意,如果使用不当,这种类型的挂起

引用(特别是事件)是一个常见的原因。内存问题 -

即事件订阅使得收集的数千个对象无法收取。例如:
http://www.codeproject.com /KB/showca...SProfiler.aspx

Marc


Hi,

Suppose a situation where some object goes out of scope and its reference
is not kept anywhere, but a reference to some delegate (that references
one of the object''s methods) is kept as a value in some map.

Is there a possibility that the object is disposed and the delegate will
be invalid?

Yours DoB

--
Using Opera''s revolutionary e-mail client: http://www.opera.com/mail/

解决方案

On Dec 11, 12:58 pm, DoB <D...@dob.comwrote:

Suppose a situation where some object goes out of scope and its reference
is not kept anywhere, but a reference to some delegate (that references
one of the object''s methods) is kept as a value in some map.

Is there a possibility that the object is disposed and the delegate will
be invalid?

Well, it might be disposed - but it certainly won''t be garbage
collected.

The "a reference to a delegate that references one of the object''s
methods" is directly contradictory to "its reference is not kept
anywhere". The reference *is* kept in the delegate - provided it''s an
instance method, of course.

Jon


The "a reference to a delegate that references one of the object''s

methods" is directly contradictory to "its reference is not kept
anywhere". The reference *is* kept in the delegate - provided it''s an
instance method, of course.

Yes, it will be an instance method.

So...
as long as the reference to the delegate is kept, the referenced method
might be safely invoked, is that right?

DoB


Define "safely" ;-p

The delegate will prevent the object being garbage collected, but if
the object itself has been deliberately closed/disposed/whatever (by
code that thinks the object is no longer needed) then the method will
probably throw an exception when invoked.

This type of delegate (to an otherwise unreferenced object) is fine by
itself, but be aware that if used inappropriately this type of hanging
reference (especially events) is a common cause of memory issues -
i.e. event subscriptions keeping thousands of objects from being
collected. For example:
http://www.codeproject.com/KB/showca...SProfiler.aspx

Marc


这篇关于垃圾收集器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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