1

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Thomas
  • 33,544
  • 126
  • 357
  • 626
  • 3
    Did you try with a memory profiler? High end versions of VS 2010 like Premium and Ultimate have a profiler included, otherwise you could download and use for free a demo of Ants Memory profiler. – Davide Piras Mar 11 '11 at 09:55
  • 3
    Why does it matter? If you suspect you might have too many controls, you probably do. The simple fix is to get rid of some. If you absolutely can't get rid of any, then what are you going to do about the amount of memory that they use? – Cody Gray - on strike Mar 11 '11 at 09:56
  • @Cody: Another way to deal with too many controls is to use pseudo-controls. This will reduce memory use (managed memory and window/GDI object counts) and improve rendering performance. If @Thomas wants to know more about pseudo-controls, ask. – Tergiver Mar 11 '11 at 10:23
  • @Tergiver: I suppose you're talking about drawing the "control" in the `Paint` event handler, rather than using a native Windows control? Yes, that's an option, but it is **rarely** necessary. Windows supports more than a reasonable amount of controls. If you need more than that, you have too many. A better option than psuedo-controls is replacing multiple smaller controls with one larger control. Several radio buttons become a combobox, for example. Using a ListView or a TreeView can also provide you with a lot of rich functionality while still using only one control. – Cody Gray - on strike Mar 11 '11 at 10:27
  • @Cody: Rarely necessary? The poster-child for pseudo-controls is the DataGridView control. That is one massive collection of pseudo-controls. How about the PropertyGrid? Hardly rare. – Tergiver Mar 11 '11 at 12:45
  • @Tergiver: That's why I clarified what I thought you meant by "pseudo-controls". I wouldn't consider the DataGridView or PropertyGrid to be pseudo-controls. They're full-fledged controls with a window handle and everything, just like the TreeView or ListView. The fact that they're a *combination* of other controls doesn't make them any less of a control themselves. But yes, I agree that combining a lot of small controls into a single, larger, more complex control is a good solution. I mentioned as much in my last comment. – Cody Gray - on strike Mar 11 '11 at 12:54
  • @Cody: I may have an unusual bias here as I've spent most of my career writing applications for embedded systems. These are literally control surfaces for hardware. The users of these applications want every single controllable point to be exposed on a single window. They can't be bothered with tab pages or combo boxes (that's two clicks instead of one). So I have had to write a lot of pseudo- and custom-controls. Even if it is 'uncommon', it's still useful to consider when you're having control-heavy issues. – Tergiver Mar 11 '11 at 13:24

3 Answers3

3

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.

Community
  • 1
  • 1
Aliostad
  • 80,612
  • 21
  • 160
  • 208
2

What Are Some Good .NET Profilers?

Community
  • 1
  • 1
abramlimpin
  • 5,027
  • 11
  • 58
  • 97
1

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

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99