Winapi ::获取可用句柄数 [英] Winapi :: Get available handles count

查看:149
本文介绍了Winapi ::获取可用句柄数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建长时间运行我的程序的测试,并不时输出可用句柄的计数.我该如何使用某些WINAPI函数?

I want to create test that run my program for long time and output the count of available handles time to time. How can I do this with some WINAPI function?

这是一篇很棒的文章,介绍如何调试句柄泄漏 http://blogs.technet.com/b/yongrhee/archive/2011/12/19/how-to-to-troubleshoot-a-handle-leak.aspx 但这不适合我的情况.我不知道如何在测试中自动化调试器.

This is a great article of how to debug handle leaks http://blogs.technet.com/b/yongrhee/archive/2011/12/19/how-to-troubleshoot-a-handle-leak.aspx but it didn't suitable in my case. I didn't have idea of how to automate debugger in my test.

推荐答案

那不是它的工作原理.您可以使用的句柄数量受配额限制,默认情况下为10,000个句柄.共有三种类型的句柄,每种句柄由它们自己的配额控制:

That's not how it works. The number of handles you can consume is limited by a quota, by default it is 10,000 handles. There are three types of handles, each governed by their own quota:

  • 内核句柄,由kernel32.dll导出的函数返回.文件,管道,套接字,同步对象等.识别它们的最佳方法是通过释放它们,内核句柄始终需要CloseHandle().超出限制的内核句柄数没有硬上限,当内核内存池空间不足时会发生故障

  • kernel handles, returned by functions that are exported by kernel32.dll. Files, pipes, sockets, synchronization objects, etcetera. Best way to identify them is by the way they are released, kernel handles always require CloseHandle(). There is no hard upper limit on the number of kernel handles beyond the quota, failure occurs when the kernel memory pool runs out of space

user32句柄,窗口和菜单对象.超出配额后,在一个桌面会话中可以分配的句柄数量存在硬上限.在同一桌面上运行的所有进程的所有user32句柄的总和不能超过上限,它认为是65535句柄

user32 handles, window and menu objects. Beyond the quota, a hard upper limit exists for the number of handles that can be allocated in one desktop session. The sum of all user32 handles of all processes running on the same desktop cannot exceed an upper limit, it think it is 65535 handles

gdi句柄,设备上下文以及绘图对象,例如位图和画笔等.超出配额时,它受到与user32相同的硬上限.

gdi handles, device contexts and drawing objects like bitmaps and brushes, etcetera. Beyond the quota it is subject to the same hard upper limit as user32 handles.

当程序消耗三个配额限制之一时,它将始终失败.但是,如果其他进程消耗大量的user32或gdi对象,或者内核内存池承受压力,则可能会更早失败.

A program will always fail when it consumes one of the three quota limits. But can fail earlier if other processes consume a lot of user32 or gdi objects or the kernel memory pool is under pressure.

要做的明智的事情不是记录仍然可用的句柄数量,您无法找出,而是记录已消耗的句柄数量.您可以调用GetGuiResources()来跟踪消耗的user32和gdi句柄的数量. GetProcessHandleCount()返回用于您的进程的内核句柄数.

The sane thing to do is not log the number of handles still available, you can't find out, but instead log how many handles you've consumed. You can call GetGuiResources() to track the number of consumed user32 and gdi handles. GetProcessHandleCount() returns the number of kernel handles in use for your process.

但是,到目前为止,最简单的方法是使用任务管理器"的进程"选项卡,而不是编写代码.在Windows 8上,使用查看+选择列",右键单击列标题,然后勾选句柄",用户对象"和"GDI对象".在程序执行时,您将实时更新三组句柄类型的句柄计数,而在调试代码时,将立即获得反馈.

But instead of writing code, by far the simplest way is use Task Manager, Processes tab. Use View + Select Columns, on Windows 8 right-click the column headers, and tick Handles, User Objects and GDI Objects. You'll get a live update of the handle count for the three sets of handle types while your program executes and immediate feedback while you debug your code.

这篇关于Winapi ::获取可用句柄数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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