Reduce the Application Launch Time

  • 3 minutes to read

When you build a .NET application, it is compiled into Microsoft Intermediate Language (MSIL). When a user launches the application, its MSIL code is compiled into machine code by the “just-in-time” (JIT) compiler. This process can cause noticeable delays. External DLLs (for example, DevExpress DLLs) may be loaded in addition to your own application, which means that any delay does not depend on the size of your code alone.

This topic describes how to reduce the WPF or WinForms application startup time if you have noticeable delays in your application due to the JIT compilation.

.NET Framework

Compile IL Code Into Native Code

Use the Native Image Generator (Ngen.exe) tool to compile assemblies’ IL code into native code. When an end user runs your application, CLR loads the precompiled code from the native image cache, so that no compilation is required at runtime. The Ngen.exe tool ships with the .NET Framework SDK.

You can compile your application’s IL code (and its dependencies’ IL code) into native code as follows:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe install C:\MyApp.exe 

Run Ngen.exe after your application’s deployment.

Note

NGen.exe produces code that is not as highly optimized as the JIT compiler–produced code.

Refer to the following article for information about various native image generation capabilities: Ngen.exe (Native Image Generator).

Use MultiCore JIT

In .NET Framework 4.5 and higher, you can use the System.Runtime.ProfileOptimization class to enable Multicore JIT. Multicore JIT uses parallelization to reduce the JIT compilation time during application startup.

Note

Using the MultiCore JIT is not as effective as compiling IL code into native code.

The code sample below demonstrates how to enable Multicore JIT in your application.

public App() {
    // Defines where to store JIT profiles
    ProfileOptimization.SetProfileRoot(@"C:\MyAppFolder");
    // Enables Multicore JIT with the specified profile
    ProfileOptimization.StartProfile("Startup.Profile");
} 

The first time the application starts, the JIT compiler records every method it should compile. The CLR then saves a profile of the methods that were executed. The ProfileOptimization.SetProfileRoot method specifies an existing folder where the profiles are saved. Multicore JIT is not applied at this moment.

The second time the application runs, the ProfileOptimization.StartProfile method loads the specified profile from disk and uses this information to compile methods in the background before they are called from the main thread.

Refer to the following articles for more information:

.NET/.NET Core - Ready to Run Images

.NET/.NET Core allow you to reduce the application startup time by compiling your application to ReadyToRun (R2R) format.

R2R binaries contain native code similar to what the just-in-time (JIT) compiler produces. The native code is used to reduce the amount of work the JIT compiler needs to do as your application loads.

To publish an app in the R2R format, set the <PublishReadyToRun> option to true in your project file:

<PropertyGroup>
  <PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>

The R2R binaries are larger, because they contain both the native and IL code.

Refer to the following topic for more information: Deploy .NET/.NET Core Applications.