tags:

views:

66

answers:

6

.NET Bitmap class uses GDI+

I want to know how many Bitmaps I can create.

Will memory leak when create too many Bitmaps?

+1  A: 

Memory won't leak as long as you use the framework to make them. The Bitmap class is made in such a way that instances dispose themselves when finalized, as any self-respecting IDisposable does, so even forgetting to Dispose it won't cause issues.

As for how many you can make, that depends on how much memory you have to work with.

cHao
A: 

Bitmap is a GDI+ object and you get limited number of those object in your system. Of course you may adjust the system. I don't think you will get memory leak but a system exception in case you exceed that limit.

Arseny
A: 

You will might be face memory leak problem but you can fix it by writing simple code

      using (frame) {
          frame.Save(outStream, jpegCodec, parameters);
      }
Rupeshit
+2  A: 

The limit of GDI handles is 65536 per session. See http://msdn.microsoft.com/en-us/library/ms724291(VS.85).aspx

Daniel Rose
+1  A: 

MSDN says

There is a theoretical limit of 65,536 GDI handles per session. However, the maximum number of GDI handles that can be opened per session is usually lower, since it is affected by available memory.

Source: http://msdn.microsoft.com/en-us/library/ms724291%28VS.85%29.aspx

Michał Piaskowski
A: 

Windows XP and Vista - default limit

The default limit in Windows XP and Vista is 10,000. You can monitor the number of GDI objects an application has from the "task manager"

http://msdn.microsoft.com/en-us/library/ms724291(VS.85).aspx

Note: I tested a PrintPreview with HashMorePages = True until It Throw Exception (about 4800 pages), Each Page needs 2 Gdi+ objects.

x77
About memory leak: GDI handles are private to a process. When your process ends, S.O. releases all GDI objects.
x77