如何垃圾收集和范围的工作在C#中? [英] How does garbage collection and scoping work in C#?

查看:159
本文介绍了如何垃圾收集和范围的工作在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习C#<一href="http://programmers.stackexchange.com/questions/61433/resources-or-advice-useful-coming-to-c-from-python-2-7-1">coming从蟒蛇并希望知道如何在C#垃圾收集器的工作原理 - 我发现,我明白了很多关于蟒蛇,一旦我想通了,这是什么做幕后,并希望避免小白错误的排序我在第一次学习Python的时候进行。

I'm learning C# coming from python and wish to know how the C# garbage collector works - I found that I understood a lot more about python once I figured out what it was doing behind the scenes, and wish to avoid making the sort of noob errors I made at first when learning python.

我一直没能找到任何好的解释清楚,当一个产品的垃圾收集和我留下​​的如

I've not been able to find any good clear explanations of when an item is garbage collected and am left with questions such as

  1. 在会发生什么变化的对象时,它的最后一个引用超出范围时?这是否对象获取垃圾回收或者是它仍然存在,当你传递回在其被定义?范围
  2. 在什么时候是递减refernces多少?领导让我不知道它是否甚至使用引用计数或其他技术......

回答这些,或者什么实际事情将赢得饼干(或upvotes),甚至更好的明确consise介绍,即使你的答案比较其做事的蟒蛇方式更好。我不感兴趣,这是更好的,只是细节。还回答了关于<一个我原来的职位href="http://programmers.stackexchange.com/questions/61433/resources-or-advice-useful-coming-to-c-from-python-2-7-1">programmers.stackexchange将是非常美联社preciated ...

Answers to these, or even better a clear consise overview of what's actually going on will win cookies (or upvotes), and even better if your answer compares it to the python way of doing things. I'm not interested in which is better, just the details. Also answers on my original post on programmers.stackexchange would be much appreciated...

推荐答案

在DOTNET GC引擎是一个标志 - 清除引擎,而不是像你在python用来引用计数器引擎。该系统不维护引用计数给一个变量,而是运行在收藏时,它需要回收内存,标志着所有当前可访问的指针,并删除所有不可达的指针(因此超出范围)。

The dotnet GC engine is a mark-and-sweep engine rather than a reference-counter engine like you're used to in python. The system doesn't maintain a count of references to a variable, but rather runs a "collection" when it needs to reclaim RAM, marking all of the currently-reachable pointers, and removing all the pointers that aren't reachable (and therefore are out of scope).

您可以找到更多关于它是如何工作在这里:
<一href="http://msdn.microsoft.com/en-us/library/ee787088.aspx">http://msdn.microsoft.com/en-us/library/ee787088.aspx

You can find out more about how it works here:
http://msdn.microsoft.com/en-us/library/ee787088.aspx

系统找到可到达的对象由开始在特定的根的位置,就像在堆栈中全局对象和对象,并跟踪这些引用的对象,而所有这些,等引用的对象,直到它的建成一棵完整的树。这是快于它的声音。

The system finds "reachable" objects by starting at specific "root" locations, like global objects and objects on the stack, and traces all objects referenced by those, and all the objects referenced by those, etc., until it's built a complete tree. This is faster than it sounds.

这篇关于如何垃圾收集和范围的工作在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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