0

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.

  • While my app is start up, it uses ~10 000 K memory.
  • While my app is running, it uses between ~40 000 k - ~ 70 000k memory.
  • When operation is completed, it uses ~30 000 k memory.

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

4
  • You should post the MyMethod if you want an analysis of why it is possibly hogging up resources. But it might be just that the GC has not collected the memory yet. Commented Sep 16, 2010 at 8:00
  • @Albin, MyMethod is very hard for understand. I just want to know, which technologies other peoples and professionals use for resource problems. Commented Sep 16, 2010 at 8:47
  • 1
    How did you determine how much memory your application is actually using? If you just look at the “Memory (Private Working Set)” column in Task Manager, that doesn’t tell you anything. Commented Sep 16, 2010 at 8:53
  • @Timwi:I use "Memory (Private Working Set)" column, and run application. By values in this column I get memory quantity, that uses my application in different stages. But as I understand now, it's wrong way. Commented Sep 16, 2010 at 8:59

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You will quite often notice the memory usage of your application doesn't decrease after you release resources - .NET will manage your memory and decide when to release memory or allocate more
Dispose has nothing to do with memory consumption.
1

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.

Comments

1

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.

Comments

0

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.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.