dhurwitz

Hi,

I have a WinForms app which appears to be leaking memory. Hopefully someone can help me understand what is going on. I run the app, called IA, and watch the MemUsage of the process in Task Manager. Initially, it is at 27,748 K. With anything I do, it goes up and never comes down. In fact, if I do nothing, it still goes up.

For example, I click on Help/About. This opens a modal dialog box using the following code:

dlgAbout dlg = new dlgAbout();

dlg.ShowDialog();

dlg.Dispose();

In dlgAbout, the only thing that happens is that a text string is assigned to a RichTextBox - no db access or anything like that.

After closing the modal dialog, the MemUsage has risen to 28,180. Thinking maybe it will take a bit of time for the GC to reclaim the memory, I watch. An hour and 20 minutes later, with no user interaction with the app, the MemUsage has risen steadily to 28,772.

If I interact with the app in any meaningful way, say by retrieving and editing data, the MemUsage skyrockets.

What is going on here Is this a leak and if so, what can be done about it I have scoured my code and am quite certain I am closing all db connections, disposing of modal dialogs, etc.

Thanks.

Dan




Re: Windows Forms General Memory Leak?

Fábio

No, there is no memory leak, what is happening is that the JIT compiler compiles the code from Intermediate Language

to Native Code when you first use a resource from your application. And then it stays that way in memory to speed

application usage. Notice that when you first open a new window in your application it takes longer than you open

subsequent windows of the same time. That is because the IL is already compiled to Native Code into memory

when you use it a second time. That is why memory only goes up and not down.

Why JIT does this Because it doesn't want to take forever to load an application for the first time, so it will load the application as it is needed. So if you never use a, lets say, MyReportForm, it will never be loaded and time and memory

associated to loading it are inexistant. But lets say you use MyOtherReportForm repeatedly, you probably wouldn't want

it to get loaded every time you use it right

So that is basically the standard behavior of .NET applications that are compiled to CLR.

Hope you understand what is going on. If any doubts appear, just ask.

Regards,

Fabio






Re: Windows Forms General Memory Leak?

dhurwitz

Thanks for the response, but this makes no sense to me. I understand about the JIT, but it doesn't seem correct that the memory footprint will increase that much if I open the same dialog box repeatedly. I would expect it to go up the first time it is opened, and then perhaps not come down, but I would not expect it to continue increasing every time the same dialog box is opened. Likewise, if I retrieve some data, I would expect the footprint to increase. But then if I clear that data and retrieve a second, similarly sized DataSet, I would not expect the footprint to be so cumulative of the size of the two DataSets.






Re: Windows Forms General Memory Leak?

Fábio

That is what I said, It should go up only the first time it is opened and then it will not go down, that is why

when like you said "close the about form" the memory won't go down. The Dispose() method doesn't

get rid of the native code JITed into Native Code from memory for the Dialog Form.

But in the DataSet case you have to call the method Dispose() to free the memory used by the dataset.

And this will not free the JITed code for the DataSet class. But if even so the memory footprint is continuing going up, then something I don't understand is going on behind the scenes. The code for the DataSet is JITed only once. You

can get rid of the data through Disposed().






Re: Windows Forms General Memory Leak?

dhurwitz

I did not think it was necessary to call Dispose on a DataSet - that is what the GC is for. However, just to see, I added a call to Dispose on the DataSet and it made no difference. The memory footprint still keeps increasing, even when there is no user interaction.

Can this be normal behavior

Thanks for any insight.

Dan






Re: Windows Forms General Memory Leak?

dhurwitz

This gets curioser and curioser. I just made a very small sample WinForms app. It contains only a Button and a Label. Clicking the Button updates the Label with DateTime.Now.ToString(). On running the app and watching in Task Manager, the memory footprint increases every time I click the button, and it does not come down, at least within several minutes of watching. In fact, the memory increases every time I just move the mouse over the window, even without clicking.

Is this normal behavior Do all .NET Windows apps leak or is this not really a leak If not, what is it

Thanks for any clarification.

Dan






Re: Windows Forms General Memory Leak?

JRQ

If you minimize the window, does the memory usage go down It should. The garbage collector (GC) will not reclaim memory unless it needs to.





Re: Windows Forms General Memory Leak?

dhurwitz

Yes, the memory goes down if I minimize the window. Perhaps this is normal behavior that I am seeing.