loviji
loviji

Reputation: 13080

Disposing and resource management in C#

I use BackGroundWorker and ProgressBar.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;

    e.Result = MyMethod((int)e.Argument, worker, e);

}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    tStripStatus.Text = "operation Ended.";
    tStripStatus.ForeColor = Color.Green;
}

In MyMethod I use Dispose() method for necessary resources.

How can I catch what is using 30 000 k - 10 000 k=~20 000 k memory?

Upvotes: 0

Views: 1305

Answers (6)

Henk Holterman
Henk Holterman

Reputation: 273244

How can I catch what is using 30 000 k - 10 000 k=~20 000 k memory?

By using a memory Profiler. But consider:

  • looking at a simple metric (Taskmanager) for 'memory consumption' is close to meaningless
  • you probably don't have a problem
  • it is unrelated to the Bgw

And for good measure:

  • Your Completed handler is not checking for errors. Could lead to nasty bugs.

Upvotes: 1

Madeleine
Madeleine

Reputation: 2182

You could use ANTS memory profiler to have a look what is sitting in memory. Its a great tool for hunting for memory leaks etc - download a trial here: http://www.red-gate.com/products/ants_memory_profiler/index.htm

Is it possible that in MyMethod() you have perhaps hooked up an event from an external class which is not being unhooked up when you are complete with the method?

But as Keith mentioned, objects remain in memory until the Garbage collector runs. As long as they are properly disposed of they will be released when necessary. Even calling GC.Collect() does not guarantee that the Garbage collector will run immediately.

Upvotes: 0

HCL
HCL

Reputation: 36775

You can try VMMap from Sysinternals. it's a free MS-tool that lets you analyse the memory-usage of a process.

If you are not very familiar with profiling an app, try this great video:

http://www.microsoftpdc.com/2009/CL11

It has a part about memory-analysis. As already written, don't count too much on values given by taskmanager and ProcessInfo. Because GC does not work immediately, there is a good chance that deallocation is not done yet because of efficiency.

Upvotes: 1

Keith
Keith

Reputation: 155692

Calling Dispose() in .Net doesn't immediately collect the memory - it leaves it until it's not busy doing something else.

Basically it hasn't collected that 20MB because that wasted memory isn't slowing it down yet. Your machine probably has GB free, why stop and tidy up when there's still plenty of space?

Call GC.Collect() to force it, but note that this is usually slower than leaving .Net to do its thing - .Net is quite good at only collecting when it has to, as long as you've disposed of the resource.

Upvotes: 2

Bobby
Bobby

Reputation: 11576

If you wanna know what takes up this space, you'll have to use a Memory Profiler, like ProfileSharp.

Upvotes: 0

Itay Karo
Itay Karo

Reputation: 18286

The CLR is responsible for the memory management so if you are not using external resources from your code the memory will be freed when the Garbage Collector will run.

Upvotes: 0

Related Questions