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 no 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.