.NET 1.1(也可能是1.0)线程泄漏“事件”处理。 [BUG ??] [英] .NET 1.1 (and possibly 1.0 also) Threads leaking "Event" handles. [BUG??]

查看:53
本文介绍了.NET 1.1(也可能是1.0)线程泄漏“事件”处理。 [BUG ??]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




.NET Framework 1.1中的线程(也可能在1.0中)泄漏事件

处理,按事件处理我使用来自 www.sysinternals的ProcessExplorer来监视可以监视的Win32事件句柄

。 com
,或者甚至简单地看看

好​​的旧窗口TaskManager中的句柄数。

为了演示这个问题,我所做的一切都是基本的Win表格

申请和主要实现为:


[STAThread]

static void Main()

{

Application.Run(new Form1());

}


和Form'的构造函数为

public Form1()

{

while(true)

{

Thread thread = new Thread(new ThreadStart(ThreadProc));

thread.Start();

Thread.Sleep(1000);

GC 。收藏();

}

}

和t ThreadProc简单地实现为

protected void ThreadProc()

{

Thread.Sleep(100);

返回;

}


现在让它运行一段时间并使用

ProcessExplorer或TaskManager监控句柄计数可以看出线程计数变得更多

或更小一点之后不变(这意味着在
中达到均衡,正在创建的线程和死亡的线程是同样),你会注意到句柄数量不断增加的
。我已经监控了多达10000

的句柄,之后我杀死了该应用程序。

现在,这似乎是一个bug。有人为此得到了解释和

可能是一种解决方法。顺便说一下,我也尝试将Main上的属性从

STAThread更改为MTAThread而行为没有变化。

任何帮助/建议都会受到赞赏。


谢谢,

Rohit

Hi,

Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak "Event"
handles, by Event handles I mean Win32 Event handles which can be monitored
using the ProcessExplorer from www.sysinternals.com, or even simply look at
the Handle count in the good old windows TaskManager.
To demonstrate the problem, all I did was created a basic Win Forms
application and with Main implemented as:

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

and Form''s constructor as
public Form1()
{
while(true)
{
Thread thread = new Thread( new ThreadStart( ThreadProc) );
thread.Start();
Thread.Sleep(1000);
GC.Collect();
}
}
and the ThreadProc is implemented simply as
protected void ThreadProc()
{
Thread.Sleep(100);
return;
}

Now you let it run for a while and monitor the handle count using the
ProcessExplorer or TaskManager to can see that the thread count becomes more
or less constant after a bit (which means that an equilibrium is reached in
which the threads being created and thread which die is same), you will note
that the Handle count keeps on increasing. I have monitored upto 10000
handles after which I killed the application.
Now, this seems like a bug. Has someone got any explaination for this and
possibly a workaround. BTW, I also tried changing the attribute on Main from
STAThread to MTAThread with no change in behavior.
Any help/suggestions would be appreciated.

thanks,
Rohit

推荐答案

请从控制台类型的应用程序中尝试这个,然后你会看到

没有''event''处理与Thread启动委托相关的泄漏。

如果你的代码中有其他地方有泄漏,可以你发布一个完整的

repro样本。

Willy。


" Rohit" <无**** @ nospam.org>在消息中写道

news:uM ************** @ TK2MSFTNGP11.phx.gbl ...
Please, try this from a console type application, and you will see that
there are no ''event'' handle leaks related to the Thread start delegate.
If there''s a leak is somewhere else in your code, could you post a complete
repro sample.
Willy.

"Rohit" <no****@nospam.org> wrote in message
news:uM**************@TK2MSFTNGP11.phx.gbl...

.NET Framework 1.1中的线程(也可能在1.0中)泄漏事件处理,通过事件句柄我的意思是Win32事件句柄,可以使用ProcessExplorer监视
www.sysinternals.com ,甚至只需查看
的处理好老的Windows任务管理器。
为了演示这个问题,我所做的只是创建了一个基本的Win Forms
应用程序,并且主要实现为:

[STAThread]
静态void Main()
{Application.Run(new Form1());
}

和Form'的构造函数为
public Form1( )
{
while(true)
{
Thread thread = new Thread(new ThreadStart(ThreadProc));
thread.Start();
Thread.Sleep(1000);
GC.Collect();
}
}
并且ThreadProc简单地实现为
protected void ThreadProc()
{
Thread.Sleep(100);
返回;
}
现在你让它运行一段时间并使用
ProcessExplorer或TaskManager来监控句柄计数,以便可以看到线程数变为
more或者一点之后变得不那么恒定(这意味着达到均衡的
,其中创建的线程和死亡的线程相同),你将
注意Handle计数继续增加。我已经监控了多达10000个句柄,之后我杀死了该应用程序。
现在,这似乎是一个错误。有人为此得到了解释,可能是一种解决方法。顺便说一下,我也尝试将Main
的属性从STAThread更改为MTAThread而行为没有变化。
任何帮助/建议都将不胜感激。

谢谢,
Rohit
Hi,

Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak "Event"
handles, by Event handles I mean Win32 Event handles which can be monitored using the ProcessExplorer from www.sysinternals.com, or even simply look at the Handle count in the good old windows TaskManager.
To demonstrate the problem, all I did was created a basic Win Forms
application and with Main implemented as:

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

and Form''s constructor as
public Form1()
{
while(true)
{
Thread thread = new Thread( new ThreadStart( ThreadProc) );
thread.Start();
Thread.Sleep(1000);
GC.Collect();
}
}
and the ThreadProc is implemented simply as
protected void ThreadProc()
{
Thread.Sleep(100);
return;
}

Now you let it run for a while and monitor the handle count using the
ProcessExplorer or TaskManager to can see that the thread count becomes more or less constant after a bit (which means that an equilibrium is reached in which the threads being created and thread which die is same), you will note that the Handle count keeps on increasing. I have monitored upto 10000
handles after which I killed the application.
Now, this seems like a bug. Has someone got any explaination for this and
possibly a workaround. BTW, I also tried changing the attribute on Main from STAThread to MTAThread with no change in behavior.
Any help/suggestions would be appreciated.

thanks,
Rohit



Rohit写道:


.NET中的线程框架1.1(也可能在1.0中)泄漏
事件句柄,通过事件句柄我的意思是Win32事件句柄,可以使用来自 www的ProcessExplorer来监视。 sysinternals.com ,或者甚至只是简单地看看好旧窗口中的Handle计数
TaskManager。
为了演示这个问题,我所做的就是创建一个基本的Win Forms <应用程序和主要实现为:

[STAThread]
static void Main()
Application.Run(new Form1()); <公式Form1()

{
while(true)
{
线程>}

和Form'的构造函数thread = new Thread(new ThreadStart(ThreadProc));
thread.Start();
Thread.Sleep(1000);
GC.Collect();
Hi,

Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak
"Event" handles, by Event handles I mean Win32 Event handles which
can be monitored using the ProcessExplorer from www.sysinternals.com,
or even simply look at the Handle count in the good old windows
TaskManager.
To demonstrate the problem, all I did was created a basic Win Forms
application and with Main implemented as:

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

and Form''s constructor as
public Form1()
{
while(true)
{
Thread thread = new Thread( new ThreadStart( ThreadProc) );
thread.Start();
Thread.Sleep(1000);
GC.Collect();




当您向GC.Collect /和/ GC.WaitForPendingFinalizers添加

explict调用时,我看不到任何示例代码的_real_问题。线程有一个

受保护的Finalize并没有实现IDisposable接口,因此它的

非托管句柄(似乎对应于Win32 Thread句柄和

极少数事件句柄)在GC到达它之前不要清理。

并且无限循环创建线程,你的测试不会做GC

直到它确定迫切需要这样做。


所以这不是一个真正的错误,只是一个''有趣''的设计决定不提供

线程的IDisposable。


BTW任何人都有一个很好的方法来强制运行.NET进程''GC做一个

收藏?

-

问候,

Mark Hurd,B.Sc。(Ma。)(荣誉)



I cannot see a _real_ problem with any of your sample code when you add
explict calls to GC.Collect /and/ GC.WaitForPendingFinalizers. Thread has a
Protected Finalize and does not implement the IDisposable interface, so its
unmanaged handles (which seem to correspond to a Win32 Thread handle and a
handful of Event handles) don''t get cleaned up until the GC get around to it.
And with an infinite loop creating threads, your test isn''t going to do GC
until it determines there''s a pressing need to do so.

So it''s not really a bug, just an ''interesting'' design decision to not provide
IDisposable for Thread.

BTW Anyone got a good way of forcing a running .NET process'' GC to do a
collection?
--
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)


>顺便说一句,任何人都有一个强大的方法来强制运行.NET进程''GC来做一个
> BTW Anyone got a good way of forcing a running .NET process'' GC to do a
集合?


GC.Collect()和GC.WaitForPendingFinalizers()会做到这一点,但是如果你想要

来减少工作集你必须采取行动流程类。这是我的

样本: http ://blogs.geekdojo.net/richard/posts/338.aspx


Richard


-

C#,.NET和复杂自适应系统:
http:// blogs.geekdojo.net/Richard

" Mark Hurd" <毫安****** @ ozemail.com.au>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ... Rohit写道:
collection?
GC.Collect() and GC.WaitForPendingFinalizers() will do it, but if you want
to reduce the working set you have to act on the Process class. Here''s my
sample: http://blogs.geekdojo.net/richard/posts/338.aspx

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Mark Hurd" <ma******@ozemail.com.au> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Rohit wrote:


.NET Framework 1.1中的线程(也可能在1.0中)泄漏
事件句柄,通过事件句柄我的意思是Win32事件句柄,可以使用来自 www的ProcessExplorer来监视。 sysinternals.com ,或者甚至只是简单地看看好旧窗口中的Handle计数
TaskManager。
为了演示这个问题,我所做的就是创建一个基本的Win Forms <应用程序和主要实现为:

[STAThread]
static void Main()
Application.Run(new Form1()); <公式Form1()

{
while(true)
{
线程>}

和Form'的构造函数thread = new Thread(new ThreadStart(ThreadProc));
thread.Start();
Thread.Sleep(1000);
GC.Collect();
我看不到当您向GC.Collect /和/ GC.WaitForPendingFinalizers添加
explict调用时,您的任何示例代码都会出现_real_问题。线程有
Hi,

Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak
"Event" handles, by Event handles I mean Win32 Event handles which
can be monitored using the ProcessExplorer from www.sysinternals.com,
or even simply look at the Handle count in the good old windows
TaskManager.
To demonstrate the problem, all I did was created a basic Win Forms
application and with Main implemented as:

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

and Form''s constructor as
public Form1()
{
while(true)
{
Thread thread = new Thread( new ThreadStart( ThreadProc) );
thread.Start();
Thread.Sleep(1000);
GC.Collect();
I cannot see a _real_ problem with any of your sample code when you add
explict calls to GC.Collect /and/ GC.WaitForPendingFinalizers. Thread has



a受保护的Finalize并没有实现IDisposable接口,所以
它的非托管句柄(似乎对应于Win32线程句柄和
少数事件处理)在GC到达
之前不要清理干净。并且通过无限循环创建线程,你的测试不会做GC直到它确定迫切需要这样做。

所以它不是真的一个bug,只是一个''有趣''的设计决定,而不是
为Thread提供IDisposable。

BTW任何人都有一个强大的方法来强制运行.NET进程''GC做一个
收藏?
-
问候,
Mark Hurd,B.Sc。(马)(荣誉)


a Protected Finalize and does not implement the IDisposable interface, so its unmanaged handles (which seem to correspond to a Win32 Thread handle and a
handful of Event handles) don''t get cleaned up until the GC get around to it. And with an infinite loop creating threads, your test isn''t going to do GC
until it determines there''s a pressing need to do so.

So it''s not really a bug, just an ''interesting'' design decision to not provide IDisposable for Thread.

BTW Anyone got a good way of forcing a running .NET process'' GC to do a
collection?
--
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)



这篇关于.NET 1.1(也可能是1.0)线程泄漏“事件”处理。 [BUG ??]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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