.NET memory leak?

I have an MDI that has a child form. The child form has a DataGridView . I am loading a huge amount of data into a datagrid view. When I close the child form the delete method is called in which I position the datagridview

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

      

When I close the form, the memory doesn't go down. I am using .NET Profiler to track memory usage. I see that memory usage increases when I initially load the data grid (as expected) and then becomes constant when the load is complete.

When I close the form, it remains constant. However, when I take a snapshot of memory using the memory profiler, it jumps to what it was before the file was loaded. When using a snapshot, it forces the garbage collector to run.

What's happening? Is there a memory leak? Or do I need to run the garbage collector efficiently?

Additional information :

When I close the form, I no longer need the information. This is why I am not keeping the data link.

Update

It is a requirement to download all data at the same time. Memory usage goes very high when there is a lot of data, so I wonder if I am doing something wrong and the garbage collector does not start, but on the other hand when I look at the profiler it shows that when required it goes down memory usage. So I can't figure out what's going on.

+2


source to share


5 answers


Setting a variable to null

does not magically force the garbage collector to exit. GC'ing is an expensive process and should be avoided unless absolutely necessary, so the collector only works as needed and when needed.

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

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

      



However, as Matthew Charlie pointed out, does it really matter if the CLR clings to that memory? If you force-free it, then the next time your DataGrid is full, the CLR needs to reallocate the same amount of memory, which is slow.

If your DataGrid isn't consuming close or more physical memory on your machine, leave the CLR alone - it almost certainly knows better when it comes to memory management.

+6


source


This is normal. The garbage collector runs on its own time as needed. The fact that it falls back to normal when the garbage collector enforces means that there is no leak or permalinks to keep things going.

The real question is, is a garbage collector needed? Are you using more RAM than physically? If not, is it really valid if you're using a bunch of physical memory that doesn't need anything else?



Another very good question: do you really need to load all the data into your application at once? But there is no way to answer this without additional information.

+5


source


Garbage collection doesn't happen because you didn't provide a link. If you mind this is no longer a link, it will be collected at some point. Just because it's still around for a while doesn't mean you have a memory leak. Moreover, even if you do garbage collection, the memory will not necessarily be immediately released to the OS, so you may not notice the drop in memory usage for your application.

+2


source


GC.WaitForPendingFinalizers();

- it helped me.

-1


source


This is a useful tool for tracking memory leaks:

SysInternals Process Explorer

-2


source







All Articles