如何检测窗体上的所有控件使用了多少内存?
假设我在 Windows 窗体上有很多控件。有没有办法知道每个控件使用了多少内存?有没有可以显示内存信息的免费工具?谢谢。
Suppose I have many controls on Windows Form. Is there any way to know how much memory is used for each control? Are there any free tools that can display the memory info? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Windows 窗体控件不仅仅是内存。事实上,由于内存如此便宜且可用,它很少成为瓶颈 - 特别是托管内存。
您需要记住,Windows 窗体控件使用 WIN32 窗口对象,其中包含窗口句柄和各种 WIN32 GDI 对象。它们还作为托管对象存在于堆上,但托管内存使用量不会很大。
为了回答您的问题,您可以使用内存分析器。
Windows forms controls are not just memory. In fact with memory being so cheap and available, it is rarely the bottleneck - especially managed memory.
You need to bear in mind that Windows forms controls are using WIN32 window object which contains window handles and various WIN32 GDI objects. They also have a presence on the heap as a managed object but the managed memory usage is not going to be huge.
In order to answer your question, you can use memory profilers.
什么是好的 .NET 分析器?
What Are Some Good .NET Profilers?
您可以获得有关控件内存使用情况的信息的唯一方法是每个控件都旨在提供此类信息。然而,大多数简单的类(例如“string”)不提供它,您必须猜测或谷歌来确定它的大小。
至于 GUI 控件 - 有些是 HWND 绑定的,有些则不是。有些附有其他手柄,有些则没有。有些具有一些共享资源,因此控件(或组)的第一个实例将增加内存使用量,第二个实例不会,依此类推......
Only way that you could get an info about controls' memory usage is that each control is designed to provide such an information. However, most simple classes such as 'string' doesn't supply it, and you have to either guess or google for determining its size.
As for GUI controls - some are HWND bound, some are not. Some have some other handles attached to them, some don't. Some have some shared resources, so first instance of the control (or group) will increase memory usage, second instance won't, and so on...