如何计算应用程序中的句柄数 [英] How do I count the number of handles in my application

查看:94
本文介绍了如何计算应用程序中的句柄数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C#2008编写的大型Windows窗体应用程序(在事件过程中我也使用了C#2013,同样的问题出现了。)



当手柄数量超过10.000时应用程序崩溃非常不合理



作为短期解决方案,我想在手柄数量超过8000时警告用​​户



我试过:

I have a large windows forms application written in C# 2008 (In the course of events I also used C# 2013, and the same problem occured).

The application crashes quite ungracefully when the number of handles exceeds 10.000

As a short term solution I want to warn the user when the number of handles exceed 8000

I tried:

int NumberOfHandles = System.Diagnostics.Process.GetCurrentProcess().HandleCount;



但是我获得了许多句柄(例如286),这些句柄与任务管理器中的句柄数量完全不同(在本例中为3107,尽管实际上它的用户对象的阈值为10.000)。



我当前的问题:如何从我的应用程序中测量taskmanager中显示的用户对象数量?



我在Windows 7中使用taskmanager,在使用Windows 8时,我的应用程序以相同的方式崩溃,但我无法让任务管理器向我显示用户对象的数量。



真正的解决方案是减少我的应用程序使用的句柄数量,但这将是一个不同的问题。简而言之,我创建了一个表单,在代码中我在这个表单上绘制了很多面板。每次更改信息时,都会重新绘制面板,但不会处理旧面板的手柄。如果有人可以根据这个非常简短的描述给我指示,那么非常欢迎!


But I get a number of handles (e.g 286) that is quite different from the number of handles in Task Manager (3107 in this example, although actually it's user objects that has the treshold of 10.000).

My current question: How can I measure the number of User-objects as shown in taskmanager from my application?

I use the taskmanager in Windows 7, when using Windows 8 my application crashes in the same way, but I haven't been able to let task manager show me the number of user objects.

The real solution is to decrease the number of handles my application uses, but that will be a different question. In short I create a form, and in code I paint a lot of panels on this form. Every time the information is changed, the panels are repainted, but the handles to the old panels are not disposed. If someone can give me direction based on this very short description they are more than welcome!

推荐答案

从Windows 2000开始,有一个硬编码的句柄限制--10000。 (检查HKLM \Software \ Microsoft \ Windows NT \ CurrentVersion \ Windows)。

此限制用于防止错误设计/开发的应用程序降低整个操作系统...

因此,理查德告诉你必须处理你的应用程序...但是如果你到了那个点,没有办法,你需要超过这个限制,你应该阅读'推动Windows的限制'来自Mark Russinovich的博客系列,其中一个部分是关于句柄: http:/ /blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx [ ^ ]
Since Windows 2000, there is a hard coded limit of handles - 10000. (Check HKLM\Software\Microsoft\Windows NT\CurrentVersion\Windows).
This limit exists for prevent wrongly designed/developed applications to bring down the whole OS...
So as Richard told you have to work on your application...However if you got to that point that no way around and you need more than that limit, you should read the 'Pushing the Limits of Windows' blog series from Mark Russinovich, where one of the parts is about handles: http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx[^]


请参阅我对解决方案1的评论。



您完全正确地尝试处理。事情就是这样:在一个典型的pure-.NET应用程序中产生越来越多的句柄(或者甚至更多或更少的纯粹),大多数或所有问题都存在于运行时类型实现的对象中 System.IDisposable的。检查您使用的每种类型,检测实现该接口的那些类型并为您将忘记的所有对象调用 System.IDisposable.Dispose 是至关重要的(松散可达性): https:/ /msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx [ ^ ]。



在所有情况下都要处理这些对象很重要,即使抛出异常,也不要没有。对于本地对象,以安全方式自动化它的方法是使用使用语句(不要与混淆使用指令!): https://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ]。



这是纯粹的语法糖,但非常方便,是良好编程风格的必备条件。



-SA
Please see my comment to Solution 1.

You are perfectly right in your attempt to work at disposing. Here is the thing: in a typical pure-.NET application producing more and more handles (or even more or less pure), most or all the problems lie in the objects with the runtime types implementing System.IDisposable. It is critical to check up each and every type you use, detect those implementing that interface and call System.IDisposable.Dispose for all objects which you are going to "forget" (loose reachability): https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^].

It's important to dispose such objects in all cases, no ifs no buts, even if an exception is thrown. For local objects, the way to automate it in a secured way is using using statement (not to be confused with using directive!): https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

This is pure syntactic sugar, but is very convenient and a must for good programming style.

—SA


感谢这个问题(这个问题已经回答了我的问题!)

Thanks to this question (the question answered my question allready!)
http://stackoverflow.com/questions/19314013/determine-number-of-gdi-handles-and-user-objects





我创建了这个代码,它给了我确切的计数。





I created this code, and it gives me exactly the count that is needed.

[System.Runtime.InteropServices.DllImport("User32")]
private extern static int GetGuiResources(IntPtr hProcess, int uiFlags);

public static int UserObjects()
{
    using (var process = System.Diagnostics.Process.GetCurrentProcess())
    {
        //var gdiHandles = GetGuiResources(process.Handle, 0);
        var userHandles = GetGuiResources(process.Handle, 1);
        return Convert.ToInt32(userHandles);
    }
}





是的,我知道我必须更正我的代码,但我确实需要这个代码测试我是否正朝着正确的方向前进!



感谢您的努力帮助我!



And yes, I do know I have to correct my code, but I do need this code to test whether I'm moving in the right direction!

Thanks for the efforts to help me!


这篇关于如何计算应用程序中的句柄数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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