App Trim in .NET Core

In ASP.NET Core, App Trim refers to a feature that automatically removes unused assemblies from the application's memory during runtime. This helps to reduce the memory footprint of the application and improve its overall performance.

What is Trimming?

When an ASP.NET Core application starts up, it loads all the required assemblies into memory. However, not all the assemblies may be needed throughout the lifetime of the application. App Trim identifies and removes the assemblies that are no longer required by the application. This process is called trimming.

The trimming process is done automatically by the .NET runtime, and it works by analyzing the code paths in the application to determine which assemblies are required and which ones are not. The unused assemblies are then removed from memory, freeing up space for other tasks.

App Trim is particularly useful in scenarios where memory usage is critical, such as in cloud-based environments where resources are limited and scaling can be expensive. It can also be used to improve the startup time of the application.

App Trim is available in ASP.NET Core 3.0 and later versions, and it is enabled by default in ASP.NET Core projects created with the .NET Core 3.0 SDK or later. However, it can be disabled or customized using the PublishTrimmed property in the project file or by using command-line options during the publishing process.

Trimming Options

PublishTrimmed, which enables trimming during the publishing process, was first introduced in .NET Core 3.0. However, the other options for trimming during publishing are only available in .NET 5 and newer versions.

To enable App Trim in the project file, open the .csproj file of your ASP.NET Core project and add the following property:

<PropertyGroup>

  <PublishTrimmed>true</PublishTrimmed>

</PropertyGroup>

Note:

  • In .NET 6 and newer versions, trimming is fully supported. However, in .NET Core 3.1 and .NET 5, it was considered an experimental feature.
  • It's worth noting that trimming is only available for applications that are published as self-contained deployments.

To learn more about the tool behind it, refer to the Trimmer documentation.