C# .NET 中的调试与发布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/90871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debug vs. release in .NET
提问by MichaelT
Continuing from my previous question, is there a comprehensive document that lists all available differences between debug and release modes in a C# application, and particularly in a web application?
继续我之前的问题,是否有一份综合文档列出了 C# 应用程序中调试和发布模式之间的所有可用差异,尤其是在 Web 应用程序中?
What differences are there?
有什么区别?
采纳答案by Joe
"Debug" and "Release" are just names for predefined project configurations defined by Visual Studio.
To see the differences, look at the Build Tab in Project Properties in Visual Studio.
“调试”和“发布”只是 Visual Studio 定义的预定义项目配置的名称。
要查看差异,请查看 Visual Studio 中项目属性中的构建选项卡。
The differences in VS2005 include:
VS2005 的不同之处包括:
DEBUG constant defined in Debug configuration
Optimize code enabled in Release configuration
调试配置中定义的调试常量
在发布配置中启用优化代码
as well as other differences you can see by clicking on the "Advanced" button
以及您可以通过单击“高级”按钮看到的其他差异
But you can:
但是你可以:
Change the build settings for Debug and Release configurations in Project Propeties / Build
Create your own custom configurations by right-clicking on the solution in Solution Explorer and selecting Configuration Manager
在项目属性/构建中更改调试和发布配置的构建设置
通过右键单击解决方案资源管理器中的解决方案并选择配置管理器来创建您自己的自定义配置
I think the behaviour of the DEBUG constant is fairly clear (can be referenced in the #if preprocessor directive or in the ConditionalAttribute). But I'm not aware of any comprehensive documentation on exactly what optimizations are enabled - in fact I suspect Microsoft would want to be free to enhance their optimizer without notice
我认为 DEBUG 常量的行为相当清楚(可以在 #if 预处理器指令或 ConditionalAttribute 中引用)。但是我不知道关于启用了哪些优化的任何综合文档 - 事实上我怀疑微软希望在没有通知的情况下自由地增强他们的优化器
回答by Michael Stum
I'm not aware of one concise document, but:
我不知道一份简明的文件,但是:
- Debug.Write calls are stripped out in Release
- In Release, your CallStack may look a bit "strange" due to optimizations, as outlined by Scott Hanselman
- Debug.Write 调用在 Release 中被删除
- 在 Release 中,由于优化,您的 CallStack 可能看起来有点“奇怪”,正如Scott Hanselman所概述的
回答by Hallgrim
Drawing with GDI+ is considerably slower in Debug mode.
在调试模式下,使用 GDI+ 绘图要慢得多。
回答by Dani
Release version:
发布版本:
is considerable faster (most important), optimized
can't be debuged (step by step)
and code written in "debug" directive is not included
相当快(最重要),优化
无法调试(一步一步)
并且不包括在“调试”指令中编写的代码
See What's the difference between a Debug vs Release Build?.
请参阅调试与发布版本之间的区别是什么?.
回答by andleer
One major performanance area if you are using any of the ASP.NET Ajax controls: debug information is removed from the JavaScript library when running in release, and I have seen major preformance improvements on complicated pages. Other web based resources may be either cached or not cached based on this setting.
如果您正在使用任何 ASP.NET Ajax 控件,一个主要的性能领域是:在发行版中运行时从 JavaScript 库中删除调试信息,并且我已经看到复杂页面的主要性能改进。其他基于 Web 的资源可能会根据此设置缓存或不缓存。
Also, remember that Debug / Release in a web application is dictated by the web.config
file, not your settings within Visual Studio.
另外,请记住,Web 应用程序中的调试/发布是由web.config
文件决定的,而不是您在 Visual Studio 中的设置。
<system.web>
<compilation debug="true">
More information:
更多信息:
回答by Scott Dorman
There isn't one document that lists the differences. In addition to some of the differences already listed, compiling in Debug mode turns off most of the JIT compiler optimizations that are performed at runtime and also emits more complete debug information in to the symbol database file (.pdb).
没有一份文件列出了这些差异。除了已经列出的一些差异之外,在调试模式下编译会关闭大多数在运行时执行的 JIT 编译器优化,并且还会将更完整的调试信息发送到符号数据库文件 (.pdb)。
Another big difference is that the GC behavior is somewhat different in that the JIT compiler will insert calls to GC.KeepAlive() as appropriate/neededin order to support debugging sessions.
另一个很大的区别是 GC 行为有些不同,因为 JIT 编译器将根据需要/需要插入对 GC.KeepAlive() 的调用,以支持调试会话。
回答by mili
I got an error message when I distribute executable file to another machine indicating that the system missed MSVCP110D.dll.
当我将可执行文件分发到另一台机器时,我收到一条错误消息,表明系统错过了 MSVCP110D.dll。
The solution to this issue is stated in Stack Overflow question Visual Studio MSVCP110D.dll is missing.
Stack Overflow question Visual Studio MSVCP110D.dll is missing 中说明了此问题的解决方案。
IN XXXXD.dll D means that the DLL file is a debug version of the DLL file. But MS Visual C++ Redistributable packages include only the release version of DLL files.
IN XXXXD.dll D 表示 DLL 文件是 DLL 文件的调试版本。但是 MS Visual C++ Redistributable 包仅包含 DLL 文件的发布版本。
That means if you need to distribute a program developed by Visual C++you need to build it in Release mode.And also you need to install MS Visual C++ Redistributable (correct version) on the target machine.
这意味着如果您需要分发由Visual C++开发的程序,您需要在发布模式下构建它。并且您还需要在目标机器上安装 MS Visual C++ Redistributable(正确版本)。
So I think this a one of key difference between debug and release mode.
所以我认为这是调试和发布模式之间的主要区别之一。
回答by Samuel Ptheitroadier
You can also manage some part of code that you want to run only in debug or only in release with preprocessor markups:
您还可以使用预处理器标记管理只想在调试中运行或仅在发布中运行的某些代码部分:
#if DEBUG
// Some code running only in debug
#endif
or
或者
#if NOT DEBUG
// Some code running only in release
#endif
回答by Chetan Naithani
Debug and Release are just labelling for different solution configurations. You can add others if you want. If you wish you can add more configurations from configuration manager–
Debug 和 Release 只是针对不同解决方案配置的标签。如果需要,您可以添加其他人。如果您希望可以从配置管理器添加更多配置 -
http://msdn.microsoft.com/en-us/library/kwybya3w.aspx
http://msdn.microsoft.com/en-us/library/kwybya3w.aspx
Major differences –
主要区别——
In a debug DLL several extra instructions are added to enable you to set a breakpoint on every source code line in Visual Studio. Also, the code will not be optimized, again to enable you to debug the code. In the release version, these extra instructions are removed.
PDB file is created in only Debug mode and not in release mode.
In release mode, code is optimized by the optimizer that's built into the JIT compiler. It makes the following optimizations:
? Method inlining - A method call is replaced by the injecting the code of the method.
? CPU register allocation - Local variables and method arguments can stay stored in a CPU register without ever (or less frequently) being stored back to the stack frame
? Array index checking elimination - An important optimization when working with arrays (all .NET collection classes use an array internally). When the JIT compiler can verify that a loop never indexes an array out of bounds then it will eliminate the index check.
? Loop unrolling - Short loops (up to 4) with small bodies are eliminated by repeating the code in the loop body.
? Dead code elimination - A statement like if (false) { /.../ } gets completely eliminated.
? Code hoisting- Code inside a loop that is not affected by the loop can be moved out of the loop.
? Common sub-expression elimination. x = y + 4; z = y + 4; becomes z = x
在调试 DLL 中添加了几个额外的指令,使您能够在 Visual Studio 的每个源代码行上设置断点。此外,不会优化代码,再次使您能够调试代码。在发行版中,删除了这些额外的说明。
PDB 文件仅在调试模式下创建,不在发布模式下创建。
在发布模式下,代码由内置在 JIT 编译器中的优化器进行优化。它进行了以下优化:
? 方法内联 - 方法调用被注入方法的代码所取代。
? CPU 寄存器分配 - 局部变量和方法参数可以一直存储在 CPU 寄存器中,而不会(或不那么频繁地)存储回堆栈帧
? 数组索引检查消除 - 处理数组时的一项重要优化(所有 .NET 集合类在内部使用数组)。当 JIT 编译器可以验证循环永远不会索引数组越界时,它将消除索引检查。
? 循环展开 - 通过重复循环体中的代码来消除具有小体的短循环(最多 4 个)。
? 死代码消除 - 像 if (false) { /.../ } 这样的语句被完全消除。
? 代码提升 - 循环内不受循环影响的代码可以移出循环。
? 常见的子表达式消除。x = y + 4; z = y + 4; 变成 z = x