DEV Community

Mauro Petrini 👨‍💻
Mauro Petrini 👨‍💻

Posted on

📦 Reduce the size of your app in .NET Core 3 and above


Welcome! Spanish articles on LinkedIn. You can follow me on Twitter for news.


If you want to reduce the size of your app developed in .NET Core 3, you are in the right article!

New options to publish our app 👨‍💻👩‍💻

.NET Core 3 SDK comes with a tool that can reduce the size of apps by analyzing IL and trimming unused assemblies 📦.

The MSBuild property to apply this feature is called PublishTrimmed.

When we enabled the option, we have to publish our app as a self-contained that includes the .NET Core runtime, libraries, the application, and its dependencies.

It is often useful because the application can run on a machine that doesn't have the .NET Core runtime installed.

Let's dive into a small sample 🥳

I've created a simple console application with the new .NET 5.0 preview 3 🤯 using dotnet CLI

    dotnet new console -n test_net5

The .csproj file should be looks like

    <Project Sdk="Microsoft.NET.Sdk">

      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
      </PropertyGroup>

    </Project>

Add PublishSingleFile property

First, we're going to add the option to publish our console application as a single file (.exe).

    <Project Sdk="Microsoft.NET.Sdk">

      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
        <Configuration>Release</Configuration>

        <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
        <PublishSingleFile>true</PublishSingleFile>
      </PropertyGroup>

    </Project>

PublishSingleFile is a new option to generate a single executable for our application. We have to include a rid (aka runtime identifier) to specify the target platform, in this case, for Windows 10 / Windows Server 2016 operating system.

Publish our application

Run the following command in the console to publish our application.

    dotnet publish

The result is an executable called test_net5.exe with a size of 68,204,270 bytes (84 MB on disk) 😲

Let's reduce the size of our application.

We are going to add PublishTrimmed property to our .csproj file and set the value to true.

The final .csproj file should look like

    <Project Sdk="Microsoft.NET.Sdk">

      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
        <Configuration>Release</Configuration>

        <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
        <PublishSingleFile>true</PublishSingleFile>

        <PublishTrimmed>true</PublishTrimmed>
      </PropertyGroup>

    </Project>

Then, run the following command again in the console to publish our application.

    dotnet publish

We got the same result, but the executable now has a size of 26,073,963 bytes (26.3 MB on disk) 🤯

Conclusion

As I said before, this new option allows us to reduce the size of the app by analyzing IL and trimming unused assemblies. We have to be careful because if our application use reflection or implement some dynamic behavior, it is probably going to break when trimmed because the linker doesn't know about this dynamic behavior and can't determine which framework types are required for reflection. The IL Linker tool can be configured to be aware of this scenario, but we have to test our app after trimming.

You can read more about IL Linker tool here to see what happen under the hood.

I hope you've enjoyed the article!

Stay safe, and keep learning!

Top comments (0)