能否符合C#编译器优化掉,如果它是唯一的强引用对象的本地(但未使用)的变量? [英] Can a conforming C# compiler optimize away a local (but unused) variable if it is the only strong reference to an object?

查看:113
本文介绍了能否符合C#编译器优化掉,如果它是唯一的强引用对象的本地(但未使用)的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参见以下相关资源:

      
  • <一个href="http://stackoverflow.com/questions/3161119/does-the-net-garbage-collector-perform-$p$pdictive-analysis-of-$c$c">Does在.NET垃圾回收器执行predictive分析code?(堆栈溢出)
  •   
  • <一个href="http://blogs.msdn.com/b/abhinaba/archive/2011/01/04/wp7-when-does-gc-consider-a-local-variable-as-garbage.aspx?wa=wsignin1.0"相对=nofollow> WP7:什么时候GC考虑一个局部变量作为垃圾(MSDN博客上的文章)
  •   
  • Does the .NET garbage collector perform predictive analysis of code? (on Stack Overflow)
  • WP7: When does GC Consider a Local Variable as Garbage (blog article on MSDN)

在换句话说:

对象可由本地引用   前一个变量进行回收   变量超出范围(如:   因为变量被分配,但   然后不再使用),或者是   保障对象为不合格的   垃圾收集,直到变量   超出范围?

Can an object referenced by a local variable be reclaimed before the variable goes out of scope (eg. because the variable is assigned, but then not used again), or is that object guaranteed to be ineligible for garbage collection until the variable goes out of scope?

让我来解释一下:

void Case_1()
{
    var weakRef = new WeakReference(new object());

    GC.Collect();  // <-- doesn't have to be an explicit call; just assume that
                   //     garbage collection would occur at this point.

    if (weakRef.IsAlive) ...
}

在此code比如说,我显然有规划的new'ed 对象被垃圾回收器回收的可能性;因此,如果语句。

In this code example, I obviously have to plan for the possibility that the new'ed object is reclaimed by the garbage collector; therefore the if statement.

(请注意,我用 weakRef 检查,如果new'ed 对象是唯一的目的仍然存在。)

(Note that I'm using weakRef for the sole purpose of checking if the new'ed object is still around.)

void Case_2()
{
    var unusedLocalVar = new object();
    var weakRef = new WeakReference(unusedLocalVar);

    GC.Collect();  // <-- doesn't have to be an explicit call; just assume that
                   //     garbage collection would occur at this point.

    Debug.Assert(weakRef.IsAlive);
}

在此code从previous一个例子,主要的变化是,new'ed 对象是强烈的局部变量(<$引用C $ C> unusedLocalVar )。然而,这个变量不会再弱引用之后使用( weakRef )已创建。

The main change in this code example from the previous one is that the new'ed object is strongly referenced by a local variable (unusedLocalVar). However, this variable is never used again after the weak reference (weakRef) has been created.

问:被允许的前两行的 Case_2 优化到这些符合标准的C#编译器 Case_1 ,如果它看到 unusedLocalVar 只用在一个地方,即作为参数传递给的WeakReference 构造?即是否有可能在断言 Case_2 都不能失败?

Question: Is a conforming C# compiler allowed to optimize the first two lines of Case_2 into those of Case_1 if it sees that unusedLocalVar is only used in one place, namely as an argument to the WeakReference constructor? i.e. is there any possibility that the assertion in Case_2 could ever fail?

推荐答案

没关系什么C#编译器 - 抖动/ GC允许清理本地引用,一旦他们不再活在一个方法体。看文档的 GC.KeepAlive

It doesn't matter what the C# compiler does - the JITter/GC are allowed to clean up local references once they're no longer alive in a method body. Look at the docs for GC.KeepAlive

此外,此<一href="http://download.microsoft.com/download/e/2/1/e216b4ce-1417-41af-863d-ec15f2d31b59/DEV490.ppt">powerpoint presentation ,特别是从幻灯片30起,帮助解释什么是JIT / GC可以得到高达。

Also, this powerpoint presentation, especially from slide 30 onwards, helps to explain what the JIT/GC can get up to.

这篇关于能否符合C#编译器优化掉,如果它是唯一的强引用对象的本地(但未使用)的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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