C#中的处理和资源管理

时间:2021-10-14 11:23:15

I use BackGroundWorker and ProgressBar.

我使用BackGroundWorker和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.

在MyMethod中,我使用Dispose()方法获取必要的资源。

  • While my app is start up, it uses ~10 000 K memory.
  • 当我的应用程序启动时,它使用~10 000 K内存。
  • While my app is running, it uses between ~40 000 k - ~ 70 000k memory.
  • 当我的应用程序运行时,它使用~40 000 k - ~70 000k内存。
  • When operation is completed, it uses ~30 000 k memory.
  • 操作完成后,使用~30 000 k内存。

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

如何捕获使用30 000 k - 10 000 k = ~20 000 k内存的内容?

6 个解决方案

#1


1  

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

如何捕获使用30 000 k - 10 000 k = ~20 000 k内存的内容?

By using a memory Profiler. But consider:

通过使用内存Profiler。但请考虑:

  • looking at a simple metric (Taskmanager) for 'memory consumption' is close to meaningless
  • 看一个简单的指标(Taskmanager)来表示“内存消耗”几乎毫无意义
  • you probably don't have a problem
  • 你可能没有问题
  • it is unrelated to the Bgw
  • 它与Bgw无关

And for good measure:

好的方法:

  • Your Completed handler is not checking for errors. Could lead to nasty bugs.
  • 您的已完成处理程序未检查错误。可能导致讨厌的错误。

#2


2  

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

在.Net中调用Dispose()不会立即收集内存 - 它会离开它直到它不忙于执行其他操作。

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?

基本上它没有收集到20MB,因为浪费的内存并没有减慢它的速度。你的机器可能有GB免费,为什么在有足够的空间时停下来整理一下?

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.

调用GC.Collect()来强制它,但请注意,这通常比让.Net完成它的速度慢得多 - .Net非常善于仅在必要时进行收集,只要你已经处理掉了资源。

#3


1  

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

您可以从Sysinternals尝试VMMap。它是一个免费的MS工具,可以让您分析进程的内存使用情况。

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

如果您对分析应用程序不是很熟悉,请尝试以下视频:

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

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.

它有关于记忆分析的一部分。如前所述,不要过多地依赖taskmanager和ProcessInfo给出的值。由于GC不能立即起作用,因此效率很高,因此尚未完成释放。

#4


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.

CLR负责内存管理,因此如果您不使用代码中的外部资源,则在垃圾收集器运行时将释放内存。

#5


0  

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

如果你想知道占用这个空间的是什么,你将不得不使用Memory Profiler,比如ProfileSharp。

#6


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

您可以使用ANTS内存分析器来查看内存中的内容。它是寻找内存泄漏等的一个很好的工具 - 在这里下载试用版: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?

是否有可能在MyMethod()中你可能已经连接了一个外部类的事件,当你完成这个方法时它没有被解开?

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.

但正如Keith所说,在垃圾收集器运行之前,对象仍然存在于内存中。只要它们被妥善处理,它们将在必要时被释放。即使调用GC.Collect()也不能保证垃圾收集器会立即运行。

#1


1  

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

如何捕获使用30 000 k - 10 000 k = ~20 000 k内存的内容?

By using a memory Profiler. But consider:

通过使用内存Profiler。但请考虑:

  • looking at a simple metric (Taskmanager) for 'memory consumption' is close to meaningless
  • 看一个简单的指标(Taskmanager)来表示“内存消耗”几乎毫无意义
  • you probably don't have a problem
  • 你可能没有问题
  • it is unrelated to the Bgw
  • 它与Bgw无关

And for good measure:

好的方法:

  • Your Completed handler is not checking for errors. Could lead to nasty bugs.
  • 您的已完成处理程序未检查错误。可能导致讨厌的错误。

#2


2  

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

在.Net中调用Dispose()不会立即收集内存 - 它会离开它直到它不忙于执行其他操作。

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?

基本上它没有收集到20MB,因为浪费的内存并没有减慢它的速度。你的机器可能有GB免费,为什么在有足够的空间时停下来整理一下?

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.

调用GC.Collect()来强制它,但请注意,这通常比让.Net完成它的速度慢得多 - .Net非常善于仅在必要时进行收集,只要你已经处理掉了资源。

#3


1  

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

您可以从Sysinternals尝试VMMap。它是一个免费的MS工具,可以让您分析进程的内存使用情况。

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

如果您对分析应用程序不是很熟悉,请尝试以下视频:

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

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.

它有关于记忆分析的一部分。如前所述,不要过多地依赖taskmanager和ProcessInfo给出的值。由于GC不能立即起作用,因此效率很高,因此尚未完成释放。

#4


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.

CLR负责内存管理,因此如果您不使用代码中的外部资源,则在垃圾收集器运行时将释放内存。

#5


0  

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

如果你想知道占用这个空间的是什么,你将不得不使用Memory Profiler,比如ProfileSharp。

#6


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

您可以使用ANTS内存分析器来查看内存中的内容。它是寻找内存泄漏等的一个很好的工具 - 在这里下载试用版: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?

是否有可能在MyMethod()中你可能已经连接了一个外部类的事件,当你完成这个方法时它没有被解开?

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.

但正如Keith所说,在垃圾收集器运行之前,对象仍然存在于内存中。只要它们被妥善处理,它们将在必要时被释放。即使调用GC.Collect()也不能保证垃圾收集器会立即运行。