I read the Garbage Collection from Jeffrey Richter's "CLR via C #". There, he illustrated an example of how the GC works conceptually (as the roots are marked), referring to a demo list of native code emitted by the JIT compiler. From this example, it occurred to me that nested reference types in scope have a ZERO effect to speed up garbage collection of a nested variable. I wonder if I understand this correctly. In any case, consider these 2 versions of code:
A) Insertion of a variable of reference type (y) in the inner area:
namespace scope { class A { public void foo() { } } class Program { static void Main(string[] args) { A x = new A(); x.foo(); { A y = new A(); y.foo(); } } } }
B) Same as above, except that x and y are in the same area.
namespace scope { class A { public void foo() { } } class Program { static void Main(string[] args) { A x = new A(); x.foo(); A y = new A(); y.foo(); } } }
Out of curiosity, I checked the generated IL code for both versions, and they are SO!
Q1: Therefore, this seems to imply that actually defining a region does not speed up garbage collection. It's right? (Note: I know about the “using” statement, but I'm only curious about the behavior of the “plain old age” on the GC, as shown in the examples above.)
Q2: If the answer to Q1 is "True", then I am completely puzzled by how the situation "Object lifetime is not determined by region" can occur, as described here: http://www.curly-brace.com/favorite.html
scope garbage-collection c #
mobbi
source share