Debug vs. release in .NET

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


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.


"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

Tags:

C#

.Net

Asp.Net