Having a memory leak type of problem

C

Chris Oates

I'm using interop to bring in user32.dll and gdi32.dll, and I'm finding that
I appear to have some kind of memory leak in the CLR. use of
GC.GetTotalMemory() shows a lot of motion, but it does not continually grow,
leaving me to think that it's a problem with my interop calls.

I have a timer set up which calls the following function 20 times per
second. It finds the color of the pixel under the mouse cursor. The real
version does more with that info, but even this simple version seems to leak
about 25K/second.

private void timer_Tick(object sender, System.EventArgs e)
{
mousePoint = Control.MousePosition;
IntPtr dc = GetDC(IntPtr.Zero);
int pc = GetPixel(dc, mousePoint.X, mousePoint.Y);
ReleaseDC(dc);
}

Any ideas?

~Chris
 
M

Mickey Williams [C# MVP]

How are you identifying that the leak is in the CLR? The simplest and surest
approach is to use a perf counter or the CLR memory profiler. If you're sure
the leak is in the managed heap, you can use WinDBG and the SOS debugger
extension to identify the blocks that are being leaked.

The problem with GC.GetTotalMemory is that you just get a number back,
without any information attached to it.
 
C

Chris Oates

I just downloaded the .NET SDK (after realizing it isnt included in Vis
Studio .NET) -- is there an FAQ on using the tools you mention? There's not
one that obviously claims to be a memory profiler.

The way that I have been seeing the problem is using Task manager. The
memory size of my task keeps growing, and eventually GUI elements (both in
my app and on the desktop) start failing to redraw properly. Use of
GetTotalMemory leads me to believe it's not GC objects that are growing
(since that number does not grow without bounds) but in unmanaged objects.

Thanks.

~Chris
 
C

Chris Oates

As ti turns out, the code I got from the C# cookbook seems to have an error.
It only linked in ReleaaseDC with one parameter (IntPtr dc) -- but, the
actual API seems to take 2 (IntPtr hwnd, IntPtr dc) -- when I fixed this,
and passed in IntPtr.Zero for the first parameter, the memory stopped
growing, and I stopped getting out of memory exceptions.

I'll see if I can dig up an errata for the cookbook.

~Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top