Because .NET Core supports self-contained deployment - runtime and framework libraries are deployed alongside the app, so the end use doesn’t have to install .NET runtime. Even though this is a great feature if you want to distribute your app to the end users, it has the downside - the size.

This is why .NET team developed mechanism called assembly trimming. During the publish process ILLink (the tool that does the trimming) “walk” though the code and identify the assemblies that are used by the code and remove the rest from the result bundle.

In .NET 5, this tool goes even deeper, it removes the types and members inside assemblies that are not used by the application, reducing the size even more.

Here is the comparison for the Hello World app (dotnet new console):

Assembly Trimming Result .NET 5Trimming is not safe

The trimming does a static analysis of the code and can only identify types and members when they are statically referenced from the code. But what about reflection? This is a catch. ILink tool will simple remove the assembly, member or type if you do not specify otherwise manually. That is why you should be really careful with enabling trimming and using it in production.

You might think that if you do not use reflection in your code, you are safe, but wait for a moment, there might be some other third party nuget package that rely on reflection. So extensive testing is a must when using trimming.

#.net #.net core #.net 5 #trim

Make Apps Small Again. Trimming in .NET 5
1.25 GEEK