johnfound johnfound - 1 year ago 109
C Question

How cheap/expensive is memory DC in Win API?

I am working on a program that will have many DIB bitmaps (created by

) and will have to draw a lot of text on them using Win API.

In order to draw on the bitmap, Windows needs device context, created by

And now here are two approaches:

  1. I can create the DC once per bitmap, using it for drawing and delete it when freeing the bitmap.

  2. Or I can create DC only when I need to draw to the bitmap, call the draw functions and delete the DC.

What is the better approach? I prefer the first, because of less calls - this will make my code much smaller and also a little bit faster.

But isn't it too expensive to hold a long living DC for every bitmap?

Edit1: The application is actually a GUI toolkit library that can be used in different and unpredictable way in the future, so I need a well balanced decision with maximal possible performance and minimal system resource usage.

Answer Source

GDI objects are limited, both per process as well as per session. You are competing for resources with all other processes running in the same session. With that in mind, you should consume GDI resources only when needed (option 2 in your question).

Mark Russinovich's blog entry Pushing the Limits of Windows: USER and GDI Objects – Part 2 goes into a fair amount of detail. To sum up the gist, here is a list of limits that the window manager places on GDI resources:

  • 10.000 GDI objects per process (default value, configurable through the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\GDIProcessHandleQuota).
  • 65.535 GDI objects per user session.
  • GDI object memory limit is the paged pool limit (see Pushing the Limits of Windows: Paged and Nonpaged Pool).
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download