Debug vs. release in .NET

C#.Netasp.net

C# Problem Overview


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?

What differences are there?

C# Solutions


Solution 1 - C#

"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.

The differences in VS2005 include:

  • 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

Solution 2 - C#

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

Solution 3 - C#

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).

Another big difference is that the GC behavior is somewhat different in that the JIT compiler will insert calls to GC.KeepAlive() as appropriate/needed in order to support debugging sessions.

Solution 4 - C#

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–

http://msdn.microsoft.com/en-us/library/kwybya3w.aspx

Major differences –

  1. 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.

  2. PDB file is created in only Debug mode and not in release mode.

  3. 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

Solution 5 - C#

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.

Also, remember that Debug / Release in a web application is dictated by the web.config file, not your settings within Visual Studio.

<system.web>
    <compilation debug="true">

More information:

Solution 6 - C#

Drawing with GDI+ is considerably slower in Debug mode.

Solution 7 - C#

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

Solution 8 - C#

Release version:

  1. is considerable faster (most important), optimized

  2. can't be debuged (step by step)

  3. and code written in "debug" directive is not included

See What's the difference between a Debug vs Release Build?.

Solution 9 - C#

I got an error message when I distribute executable file to another machine indicating that the system missed MSVCP110D.dll.

The solution to this issue is stated in 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.

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.

So I think this a one of key difference between debug and release mode.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichaelTView Question on Stackoverflow
Solution 1 - C#JoeView Answer on Stackoverflow
Solution 2 - C#Michael StumView Answer on Stackoverflow
Solution 3 - C#Scott DormanView Answer on Stackoverflow
Solution 4 - C#Chetan NaithaniView Answer on Stackoverflow
Solution 5 - C#andleerView Answer on Stackoverflow
Solution 6 - C#HallgrimView Answer on Stackoverflow
Solution 7 - C#Samuel PoirierView Answer on Stackoverflow
Solution 8 - C#DaniView Answer on Stackoverflow
Solution 9 - C#miliView Answer on Stackoverflow