.NET Core Program Slimmer Released, Compressing Program Size to 1/3

.NET Core Program Slimmer Released, Compressing Program Size to 1/3

.NET Core has the feature of [trimming unused code], but because it is implemented using static analysis, its trimming effect is not optimal.

Last updated 3/10/2022 1:12 PM
杨中科
2 min read
Category
.NET
Tags
.NET C# .NET Core Trimming Slimming

.NET Core has a feature for "trimming unused code", but because it is implemented using static analysis, its trimming effect is not optimal. It has the following two disadvantages:

  1. It does not support Windows Forms and WPF, yet desktop application developers are actually the ones with the strongest demand for the trimming feature.

  2. It cannot remove assemblies that are not used at runtime. For example, suppose our program uses assembly A, which in turn references assemblies B and C. In assembly A, only method M1 uses assembly B, and only method M2 uses assembly C. Our program only calls method M1 in A and never calls method M2 in A. Although assembly C has never been called by our program, since the "trimming unused code" feature only performs static reference checks, assembly C will still not be trimmed away.

  3. It does not handle reflection well. Because it uses static analysis, it may trim away assemblies that are only loaded via reflection at runtime.

Therefore, I developed an application for trimming .NET Core programs, which solves the issues mentioned above with .NET Core's "trimming unused code" feature. It supports Windows Forms and WPF. It analyzes the assemblies loaded by the program at runtime to determine which assemblies are unused. As a result, it can remove more unused assemblies and naturally supports reflection.

Trimming effect comparison:

Original Size .NET Built-in Trimming Zack.DotNetTrimmer
Empty Core MVC 97MB 50.3MB 43.6MB
Empty WebAPI 93MB 46.3MB 34.5 MB
Empty WPF 152 MB Not supported 75.2 MB
Empty WinForms 152 MB Not supported 50.0 MB

Project open source address: https://github.com/yangzhongke/Zack.DotNetTrimmer/

Keep Exploring

Related Reading

More Articles