How to reduce .Net Core App Size
Tags: csharp, dotnet, webdev, beginners
Published: 2021-05-17 18:11:40

###Application Size Trimming (Ref: C#)
One of the significant differences between .NET Core and .NET Framework is that .NET Core promotes self-contained deployment, i.e., everything required to run to bundle the application together. It doesn’t rely on having the framework installed separately. The drawback is the size; it pulls along a complete copy of the runtime & framework.

<iframe class="airtable-embed" src="https://airtable.com/embed/shrnlL4XcM5hOWsMZ?backgroundColor=blue&viewControls=on" frameborder="0" onmousewheel="" width="100%" height="533" style="background: transparent; border: 1px solid #ccc;"></iframe>

To resolve the size problem, Microsoft has introduced an alternative to cut unused assemblies as part of publishing self-contained applications known as “assembly linker.”

Optionally during the publishing process, a trim phase occurs, which does a walkthrough of the code classes identifying the assemblies that are used. It will then only package required assemblies into the app, thereby reducing the size of the application.
>  In .NET 5, open the assemblies, and removing the types and members that are not used by the application, further reducing the size.

For example, for a basic dot net core hello world application, the size of assemblies are:

 <iframe src="https://medium.com/media/6d48e1bd07137f792b263338db002d6f" frameborder=0></iframe>

## Assembly-level trimming

To perform trimming at assembly-level, use the dot net publish command either without a trim mode, or use <TrimMode>CopyUsed</TrimMode> in the project file:

    <Project Sdk=”Microsoft.NET.Sdk”>
     <PropertyGroup>
     <OutputType>Exe</OutputType>
     <TargetFramework>net5.0</TargetFramework>
     <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
     <PublishTrimmed>true</PublishTrimmed
     <TrimMode>CopyUsed</TrimMode>
     </PropertyGroup>
    </Project>

Or on the command line:

    dotnet publish -r win10-x64 -p:PublishTrimmed=True [-p:TrimMode=CopyUsed]

## Member-Level Trimming

.NET 5 trimming take it further deep into the code and remove types and members that are not used. It can have a significant effect where only a small subset of an assembly is used.

Further reduction in the size of application can be made by enabling Member-level trimming by putting TrimMode=Link in the project file

    <Project Sdk=”Microsoft.NET.Sdk”>
     <PropertyGroup>
     <OutputType>Exe</OutputType>
     <TargetFramework>net5.0</TargetFramework>
     <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
     <PublishTrimmed>true</PublishTrimmed
     <TrimMode>Link</TrimMode>
     </PropertyGroup>
    </Project>

Or using the command line

    dotnet publish -r win10-x64 -p:PublishTrimmed=True -p:TrimMode=Link

### More Info

* Member-level trimming has more risk than assembly-level trimming, and so is being released as an experimental feature.

* With **assembly-level trimming**, it’s more apparent when a required assembly is missing.

* With **member level trimming**, you need to have exhaustive testing of the app to ensure that nothing has been trimmed that could be required.

Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.

Follow me on [Medium](https://singhsukhpinder.medium.com/)


<a href="https://www.buymeacoffee.com/sukhpindersingh" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>