July, 2011

  • Kirill Osenkov

    How Microsoft Names Releases… And What You Can Do About It

    • 0 Comments

    image

    Microsoft® SQL Server® 2008 R2 SP1 Feature Pack CTP1.

    Scott Hanselman wrote a blog post called Request for Comments: Issues with .NET and Microsoft Product Versioning. He welcomes your feedback either in his blog comments or a Twitter Poll for you to spread around.

  • Kirill Osenkov

    In ngen’ed assemblies, static field initializers don’t run until the field is first accessed

    • 3 Comments

    Interesting fact that I discovered today: usually, static field initializers run whenever the parent type is first loaded. However when you ngen the assembly, apparently you can load the type and work with it – static field initializers will only be executed immediately before you actually access the field.

    I’ve run into this by having a static Stopwatch timer = Stopwatch.StartNew(); in one of my types. It was supposed to measure an application’s startup time, but I noticed that after NGEN’ing the assembly the timer always returned a value close to 0.

    Here’s the code to verify this behavior:

    using System;
     
    class Program
    {
        private static int field = Initialize("Field initializer");
     
        static void Main(string[] args)
        {
            Initialize("Main");
            field = 1; // touch the field
        }
     
        private static int Initialize(string message)
        {
            Console.WriteLine(message + ": " + DateTime.Now.Ticks.ToString());
            return 0;
        }
    }

    This works the same for .NET 4.0 SP1 for Debug/Release and x86/x64. I didn’t try other frameworks/OS/platforms.

    Here’s the output from the console:

    E:\>NgenedStaticFieldInitializers.exe
    Field initializer: 634462768999291588
    Main: 634462768999321594
    
    E:\>NgenedStaticFieldInitializers.exe
    Field initializer: 634462769013544438
    Main: 634462769013574444
    
    E:\>ngen install NgenedStaticFieldInitializers.exe
    Microsoft (R) CLR Native Image Generator - Version 4.0.30319.1
    Copyright (c) Microsoft Corporation.  All rights reserved.
    Installing assembly E:\NgenedStaticFieldInitializers.exe
    1>    Compiling assembly E:\NgenedStaticFieldInitializers.exe (CLR v4.0.30319) .
    ..
    1>NgenedStaticFieldInitializers, Version=1.0.0.0, Culture=neutral, PublicKeyToke
    n=null 
    
    E:\>NgenedStaticFieldInitializers.exe
    Main: 634462769095630852
    Field initializer: 634462769095660858
    
    E:\>NgenedStaticFieldInitializers.exe
    Main: 634462769108903506
    Field initializer: 634462769108933512
Page 1 of 1 (2 items)