C#中的垃圾回收未执行。为什么? [英] Garbage collection in C# not carried out. Why?

查看:128
本文介绍了C#中的垃圾回收未执行。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一个简单的实验来验证垃圾收集器的功能。引用 3.9自动内存管理 (MSDN)有关 .NET 中的自动内存管理。对我来说,它听起来像一个C ++中的共享指针。如果一个对象的引用计数器变为零,它将被垃圾回收器解除分配。

所以我尝试在我的主窗体中创建一个函数。该函数在我的主窗体的Shown事件函数中被调用,该函数在构造函数之后执行。这里是实验代码。

  public void experiment()
{
int [] a = new INT [100000];
int [] b = new int [100000];
int [] c = new int [100000];
int [] d = new int [100000];

a = null;
b = null;
c = null;
d = null;
}

以下是结果:

内存分配之前





内存分配后





离开函数范围之前





离开函数范围



< img src =https://i.stack.imgur.com/MiA6T.pngalt =http://i.stack.imgur.com/MiA6T.png>



为什么垃圾收集器不会释放数组a,b,c,d分配的内存,甚至在被设置为null之后?

解决方案.NET垃圾收集器是一个高度优化,复杂的软件野兽。它被优化,使你的程序尽可能快地运行,并使用不要太多的内存这样做。

因为释放内存的过程需要一些时间,垃圾收集器通常会等待运行它,直到程序使用大量内存为止。然后它会一次完成所有的工作,这会导致程序在相对较长时间后出现小的延迟(而不是早些时候的更小的延迟,这会减慢程序的运行速度)。

所有这些意味着垃圾收集器运行的时间不可预测



您可以多次调用您的测试一些睡眠()在循环中),并观察内存使用缓慢建立。当你的程序开始占用大部分可用物理内存时,它的内存使用量会突然下降到接近于零。



有几个函数(如 GC.Collect()),它强制执行几个级别的垃圾回收,但是强烈建议不要使用它们,除非你知道你在做什么,因为这往往让软件变得更慢,并停止垃圾收集器以最佳方式进行工作。


I have tried a simple experiment to verify the functionality of the garbage collector. Referencing 3.9 Automatic memory management (MSDN) about automatic memory management in .NET. To me, it sounded like a shared pointer equivalent in C++. If the reference counter of an object becomes zero, it will be deallocated by the garbage collector.

So I tried creating a function inside my main form. The function was called inside the Shown event function of my main form which is executed after the constructor. Here is the experimental code.

    public void experiment()
    {
        int[] a = new int[100000];
        int[] b = new int[100000];
        int[] c = new int[100000];
        int[] d = new int[100000];

        a = null;
        b = null;
        c = null;
        d = null;
    }

And here are the results:

Before memory allocation

After memory allocation

Before leaving the function scope

After leaving the function scope

Why did not the garbage collector deallocate the memory allocated by the arrays a, b, c, d even after being set to null?

解决方案

The .NET garbage collector is an highly optimized, complicated beast of software. It is optimized to make your program run as fast as possible and using not too much memory in doing so.

Because the process of freeing memory takes some time, the garbage collector often waits to run it until your program uses a whole lot of memory. Then it does all the work at once, which results in a small delay of your program after a relatively long time (instead of many smaller delays earlier, which would slow down your program).

All this means, that the time the garbage collector runs is not predictable.

You may call your test several times (with some Sleep() in the loop) and watch memory usage slowly building up. When your program begins to consume a significant portion of available physical memory its memory usage will suddenly drop to near-zero.

There are a couple of functions (like GC.Collect()) which force several levels of garbage collection, but it's strongly advised not to use them unless you know what you are doing, because this tends to make your software slower and stops the garbage collector in doing its work in an optimal way.

这篇关于C#中的垃圾回收未执行。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆