tags:

views:

429

answers:

4
+1  Q: 

.NET memory leak?

I have an MDI which has a child form. The child form has a DataGridView in it. I load huge amount of data in the datagrid view. When I close the child form the disposing method is called in which I dispose the datagridview

    this.dataGrid.Dispose();
    this.dataGrid = null;

When I close the form the memory doesn't go down. I use the .NET memory profiler to track the memory usage. I see that the memory usage goes high when I initially load the data grid (as expected) and then becomes constant when the loading is complete.

When I close the form it still remains constant. However when I take a snapshot of the memory using the memory profiler, it goes down to what it was before loading the file. Taking memory snapshot causes it to forcefully run garbage collector.

What is going on? Is there a memory leak? Or do I need to run the garbage collector forcefully?

More information:

When I am closing the form I no longer need the information. That is why I am not holding a reference to the data.

Update

It is a requirement for me to load all the data at once. The memory usage goes really high when there is a lot of data so I am wondering if I am doing something wrong and the garbage collector is not being run but on the other hand when I look at the profiler it does show that when it takes a snapshot the memory usage decreases. So I am unable to understand what is going on.

A: 

This is a useful tool for tracking down memory leaks:

SysInternals Process Explorer

Richard
-1 .NET Memory profiler (what the OP is using right now) is much better for finding memory leaks in managed code.
nikie
+4  A: 

This is normal. The garbage collector runs on it's own time, as needed. The fact that it returns to normal when the garbage collector is forced means there's no leak or permanent references keeping things around.

The real question is: Does the garbage collector need to be run? Are you using more RAM than you have physically? If not, does it really matter if you're using a bunch of physical RAM that nothing else needs?

Another really good question is, do you really need to load all the data into your application at once? But there's no way of answering that without more information.

Matthew Scharley
+2  A: 

Garbage collection does not happen just because you null the reference. If you object is no longer reference it will be collected at some point. Just because it is still around for a little while does not mean you have a memory leak. Furthermore even if you do garbage collect the memory will not necessarily be release to the OS immediately, so you may not see a drop in memory usage for the application.

Brian Rasmussen
+5  A: 

Setting a variable to null doesn't magically force the garbage collector to be invoked. GC'ing is an expensive process and should be avoided unless absolutely necessary, hence the collector only runs as and when it's scheduled to or if there is a need.

If you really need to ensure that memory is freed, manually invoke the garbage collector after you've nulled out your DataGrid:

this.dataGrid.Dispose();
this.dataGrid = null;
GC.Collect();

However, as Matthew Scharley noted, does it really matter if the CLR holds onto that memory? If you force it to be freed, then next time your DataGrid is populated the CLR needs to re-allocate the same amount of memory - which is slow.

Unless your DataGrid is consuming close to or more than the physical memory on your computer, leave the CLR alone - it almost certainly knows best when it comes to memory management.

Ian Kemp
The other thing is, even holding on to the memory, if nothing references it, it will (eventually) simply get paged out and sit on your harddrive anyway till the GC runs and collects it.
Matthew Scharley