Going Unmanaged doesn't free Memory
-
I've got a memory issue with an unmanaged call to GDI. I've followed the CreateDC, releaseDC, DeleteDC step outlined in the platform SDK,these methods all return true or 1, indicating that they succeeded. However after each call to the method it consumes 3 mb of RAM and does not release the memory, the issue is that method will be called many times and builds up to about 60mb memory used. I store a bitmap in a member variable, which i dispose of. iWidth = nWidth; iHeight = nHeight; hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()); hdcDest = GDI32.CreateCompatibleDC(hdcSrc); hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,iWidth,iHeight); image = new Bitmap(iWidth,iHeight); GDI32.SelectObject(hdcDest,hBitmap); GDI32.BitBlt(hdcDest,0,0,iWidth,iHeight,hdcSrc,iStartX,iStartY,SRCCOPY); image = Image.FromHbitmap(new IntPtr(hBitmap)); User32.ReleaseDC(User32.GetDesktopWindow(),hdcSrc); GDI32.DeleteDC(hdcDest); GDI32.DeleteObject(hBitmap); if I call GC.Collect() it clears the memory and brings it back down but this is oviously not a good idea. Any Idea's?
-
I've got a memory issue with an unmanaged call to GDI. I've followed the CreateDC, releaseDC, DeleteDC step outlined in the platform SDK,these methods all return true or 1, indicating that they succeeded. However after each call to the method it consumes 3 mb of RAM and does not release the memory, the issue is that method will be called many times and builds up to about 60mb memory used. I store a bitmap in a member variable, which i dispose of. iWidth = nWidth; iHeight = nHeight; hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()); hdcDest = GDI32.CreateCompatibleDC(hdcSrc); hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,iWidth,iHeight); image = new Bitmap(iWidth,iHeight); GDI32.SelectObject(hdcDest,hBitmap); GDI32.BitBlt(hdcDest,0,0,iWidth,iHeight,hdcSrc,iStartX,iStartY,SRCCOPY); image = Image.FromHbitmap(new IntPtr(hBitmap)); User32.ReleaseDC(User32.GetDesktopWindow(),hdcSrc); GDI32.DeleteDC(hdcDest); GDI32.DeleteObject(hBitmap); if I call GC.Collect() it clears the memory and brings it back down but this is oviously not a good idea. Any Idea's?
nEgAtIvE cReEp wrote: if I call GC.Collect() it clears the memory and brings it back down but this is oviously not a good idea That should be your clue right there. Unmanaged resources are called "unmanaged" because they are not managed by the GC. If you call
GC.Collect
and it makes a difference, then unmanaged reosurces are not your problem. The problem here is that you must dispose those images you're creating (theimage
) variable. When finished (and be re-assigningimage
to something else), callimage.Dispose()
; otherwise, theImage
is not cleaned-up until the GC gets around to it or you callGC.Collect
.Microsoft MVP, Visual C# My Articles
-
nEgAtIvE cReEp wrote: if I call GC.Collect() it clears the memory and brings it back down but this is oviously not a good idea That should be your clue right there. Unmanaged resources are called "unmanaged" because they are not managed by the GC. If you call
GC.Collect
and it makes a difference, then unmanaged reosurces are not your problem. The problem here is that you must dispose those images you're creating (theimage
) variable. When finished (and be re-assigningimage
to something else), callimage.Dispose()
; otherwise, theImage
is not cleaned-up until the GC gets around to it or you callGC.Collect
.Microsoft MVP, Visual C# My Articles
Thanks for the reply. I do dispose of the image variable, I just didn't include the code on the post. Although I realize my comment on the GC doesn't make sense. I will have to investigate if any .NET objects are still hanging around, that acompany the Call to GDI) i.e the form. thanks again