有趣的行为。 [英] Interesting behavior.

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

问题描述

请考虑以下代码。类mytest被实例化,然后设置为

null。在它的构造函数中,类实例化一个计时器,每隔5秒就会将b $ b写入Hello。奇怪的是,即使在

mytest对象被杀之后,''Hello''仍然每5秒打印一次。


这不是这个有点奇怪吗?


使用System;

使用System.Threading;

命名空间ConsoleApplication1

{

class program

{

static void Main(string [] args)

{

mytest test = new mytest(); //实例化一个对象

test = null; //杀死一个对象


Console.ReadLine();

}

}


公共课mytest

{

私人计时器计时器你好;

公共mytest()

{

timerHello =新的计时器(

新的TimerCallback(timerHello_OnTimer),

新对象(),5000,5000);

}

private void timerHello_OnTimer(Object stateInfo)

{

Console.WriteLine(" Hello");

}

}

}

Consider the code below. Class mytest is instantiated, then set to
null. In its constructor, the class instantiates a timer, which writes
out ''Hello'' every 5 seconds. The odd thing is that even after the
mytest object is killed, ''Hello'' is still printed every 5 seconds.

Isn''t this a bit odd?

using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
mytest test = new mytest(); // instantiate an object
test = null; // kill an object

Console.ReadLine();
}
}

public class mytest
{
private Timer timerHello;
public mytest()
{
timerHello = new Timer(
new TimerCallback(timerHello_OnTimer),
new object(), 5000, 5000);
}
private void timerHello_OnTimer(Object stateInfo)
{
Console.WriteLine("Hello");
}
}
}

推荐答案

Frank Rizzo< no ** @ none.comwrote:
Frank Rizzo <no**@none.comwrote:

请考虑以下代码。类mytest被实例化,然后设置为

null。在它的构造函数中,类实例化一个计时器,每隔5秒就会将b $ b写入Hello。奇怪的是,即使在

mytest对象被杀之后,''Hello''仍然每5秒打印一次。


这不是这个有点奇怪?
Consider the code below. Class mytest is instantiated, then set to
null. In its constructor, the class instantiates a timer, which writes
out ''Hello'' every 5 seconds. The odd thing is that even after the
mytest object is killed, ''Hello'' is still printed every 5 seconds.

Isn''t this a bit odd?



完全没有 - 因为对象没有被杀死。它仍然不是垃圾

,但仍然有任何实时引用,并且

Timer通过委托引用它。 (我相信不同的

计时器有不同的生命周期,例如你需要保持对System.Windows.Forms.Timer的实时

引用,否则它将停止,但是我

没有细节。)


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件

Not at all - because the object isn''t killed. It can''t be garbage
collected while there are still any live references to it, and the
Timer has a reference to it via the delegate. (I believe that different
timers have different lifetimes, e.g. you need to keep a "live"
reference to a System.Windows.Forms.Timer otherwise it will stop, but I
don''t have details.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言

新闻:MP ******************** @ msnews.microsoft.com。 ......
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .

完全没有 - 因为对象没有被杀死。它仍然不是垃圾

,但仍然有任何实时引用,并且

Timer通过委托引用它。 (我相信不同的

计时器有不同的生命周期,例如你需要保持对System.Windows.Forms.Timer的实时

引用,否则它将停止,但是我

没有详细信息。)
Not at all - because the object isn''t killed. It can''t be garbage
collected while there are still any live references to it, and the
Timer has a reference to it via the delegate. (I believe that different
timers have different lifetimes, e.g. you need to keep a "live"
reference to a System.Windows.Forms.Timer otherwise it will stop, but I
don''t have details.)



但这不是垃圾的那种循环引用问题

收藏是为了解决?


Michael

But isn''t this the very kind of circular reference issue that garbage
collection was meant to solve?

Michael


Michael,


尽管这是在解决,但它不是循环引用。垃圾

集合用于处理托管堆上的对象,而不必担心
。因为您将其设置为null,所以您已指示

该对象符合收集条件。但是,实际执行收集的时候需要

垃圾收集器。


这里有一个有趣的附注,在发布模式下,设置

对null的引用实际上延长了对象的生命周期。


- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" Michael C" < no **** @ nospam.comwrote in message

news:eD ************** @ TK2MSFTNGP06.phx.gbl ...
Michael,

It''s not a circular reference though that this is addressing. Garbage
collection is meant to take care of objects on the managed heap without you
having to worry about them. Because you set it to null, you have indicated
that the object is eligible for collection. However, it is up to the
garbage collector WHEN to actually perform the collection.

An interesting side note here, in release mode, the setting of the
reference to null actually extends the lifespan of the object.

- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael C" <no****@nospam.comwrote in message
news:eD**************@TK2MSFTNGP06.phx.gbl...

" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言

新闻:MP ******************** @ msnews.microsoft.com。 ......
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP********************@msnews.microsoft.com.. .

>完全没有 - 因为对象没有被杀死。当它还有任何实时引用时,它不能被垃圾收集,并且
Timer通过委托引用它。 (我相信不同的定时器有不同的生命周期,例如你需要保持对System.Windows.Forms.Timer的实时
引用,否则它会停止,但我不会没有详细信息。)
>Not at all - because the object isn''t killed. It can''t be garbage
collected while there are still any live references to it, and the
Timer has a reference to it via the delegate. (I believe that different
timers have different lifetimes, e.g. you need to keep a "live"
reference to a System.Windows.Forms.Timer otherwise it will stop, but I
don''t have details.)



但这不是垃圾

集合意味着什么样的循环引用问题解决?


Michael


But isn''t this the very kind of circular reference issue that garbage
collection was meant to solve?

Michael


这篇关于有趣的行为。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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