Winapi :: Get available handles count

被刻印的时光 ゝ 提交于 2019-12-12 15:04:57

问题


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?

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.


回答1:


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:

  • 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 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 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.

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.

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.

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.



来源:https://stackoverflow.com/questions/15271263/winapi-get-available-handles-count

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!