开发者

Memory usage in Winforms

I have very simple application of two forms (created to test memory). Solution has three projects. 1st having forms, 2nd DAL and 3rd ComonLib.

(All these memory figures are from Task Manager. At the end of the question there are 4 urls of images of memory-profiler)

When I run my app the memory usage is around 8MB. This form only have two buttons. One button is to open 2nd form and another button to collect GC.

When I open 2nd form the memory increase to around 550MB. This form has a grid and data (1000s of records) loads in this grid in Load event.

Code to open 2nd form.

CallWidnows cw = new CallWidnows();
cw.ShowDialog();
cw.Dispose();

Load event of 2nd form

Customers customers = new Customers();
dataGridView1.DataSource = customers.GetAllCustomers();

Close event of 2nd form.

dataGridView1.Dispose();

But after closing 2nd form 开发者_如何学JAVAno change in memory usage, and memory usage is still 550MB

After waiting a minute or so I explicitly call GC.Collect() and only few MBs of memory releases and become to 530MB

Again after waiting a minute or so I agin call GC.Collect() and now memory come down to 40MB

  • Can anyone help me to understand this memory usage behavior?

  • Why on disposing form and grid memory is not released?

  • Why on 2nd time calling GC.Collect() all the memory released but not when I called it first time?

My original application has dozens of forms and user controls and I am going through memory problem in this app.

  • Can anyone suggest few steps to analyze memory usage in Winforms app?

One side quetion: Originally I had following method as static method.

Customers customers = new Customers();
dataGridView1.DataSource = customers.GetAllCustomers();

It was like

// Static GetAllCustomers method
dataGridView1.DataSource = Customers.GetAllCustomers();
  • In my original application all DAL methods are static. Static methods can be reason of Memory Leak when used like above?

Memory Profiler Images:

1- Application Starts Memory usage 8MB

2- Second form opens and data loaded - Memory Usage - 550MB

3- Second form closed. Form disposed and grid disposed - Memory Usage still - 550 MB

4- First time GC.Collect called.

Thanks.


Two GC's to free the memory is a sign the memory is being cleaned up in finalizers (eg COM objects, anything requiring dispose to clean up). The first GC locates the objects but only schedules them for finalization. The next GC runs them. You can call WaitForPendingFinalizers after the first GC to test if this is true - it shouldn't take two GCs then.

For me, I use WinDbg with the SOS dll to understand these problems. You can also load SOS in VisualStudio but I'm more comfortable with WinDbg. The SOS dll lets you dump the objects on the heap and see why they are rooted (ie what object keeps them alive and needs to be disposed).

Not everyone has the same skill set so other suggestions for finding .net leaks can be found in this SO question: Tool for diagnosing memory leaks in .NET (ASP.NET Application).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜