C#:垃圾收集器

标签 c# garbage-collection

有必要这样做吗 poly = null; ?完成功能后,垃圾收集器会清理还是?

这里有一个简单的例子:

    public void redrawSingelPoly(Canvas canvas) 
    {
        Polyline poly = new Polyline();
        poly.Stroke = colorBrush;
        poly.StrokeThickness = basicLineThick;

        poly.Points = points;
        canvas.Children.Add(poly);

        poly = null; //Garbage Collector
    }

最佳答案

在您的示例中,您的多边形对象不会被垃圾回收。这就是为什么......

您正在执行以下操作...

canvas.Children.Add(poly);

它保留对对象的引用。即使您将本地引用设置为该对象 = null,仍然有对它的引用从方法中传递出去!

垃圾收集器只收集无法访问的对象。

也就是说 - 是否有必要设置 poly = null?不,没有必要。

关于C#:垃圾收集器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17300107/

相关文章:

c# - CPU 不像人类? 0 + 0 不比 10E12 + 9E15 简单吗?

c# - ASP.NET/WCF : Why are logical/physical threads dropping on full GC cycle?

c# - 将货币导出到 Excel 会导致空格字符

c# - 将参数传递给 ASP.NET Core 中的 jQuery 加载函数

c# - 如何在同一个项目中针对netcoreapp2.0和net461

lua - 如何显式调用lua gc?

java - JVM 在旧代为 'empty' 时运行旧代垃圾回收

php - session 结束时运行脚本

javascript - 已删除数组元素的垃圾收集

c# - 为什么我从 C# 中的串行端口得到部分奇怪的值