Get Started With Ahead of Time (AOT) Compilation in .NET

Introduction:

What is AOT?

   With Ahead Of Time(AOT) compilation, publishing your apps as self-contained gives a platform-specific executable. The publish folder contains all the components of the app, including the .NET libraries and target runtime. So, this app is completely isolated from other .NET apps and doesn’t use shared runtime. The user of the application doesn’t want to install . NET in the platform where the app has deployed. AOT apps will have faster startup time with less memory consumption.

Let’s Explore AOT with .NET 8 Application:

 I have created two console applications. One is a normal app and another app is with AOT enabled.

With the latest visual studio 2022, you can select Enable native AOT publish option for the application at the time of creation, as shown in the below figure.

Ahead of time compilation visual studio wizard

If it is .NET 7 application you can always update the project file by including PublishAot property, to enable the AOT Compilation.

Project file PublishAot property

When you package the application specify the platform where you going to publish it. In my case, it is windows x64

Use the below command to publish the application with a runtime identifier (RID) as Windows x64

dotnet publish -r win-x64 

This package includes all the runtime libraries to run the application in the windows x64 platform. To learn more about runtime identifiers, please click here.

Check the size of the package, just for a simple hello world program the packages size is around 64 MB which is huge compared to the normal app which is around.

It’s a downside of AOT, but If you check the memory consumption after the application execution, the AOT application is always on the low side on comparing with a normal app.

AOT app memory consumption – 2.7 MB. This low memory consumption is because of not using the shared .NET runtime from the platform since it has its own runtime libraries in its native package.

Normal app memory consumption – 3.2 MB

Now, AOT app play a better role in memory consumption.

Advantage of AOT

Low memory consumption with good start speed.

Summary:

 We have seen what is AOT, and how to generate a native package for the .NET application by enabling the AOT and its advantages. Let’s discuss its disadvantages in my next blog.

gowthamk91

Leave a Reply

%d bloggers like this: