Mono Linker Tool Now Reduces Size of .NET Core Apps
Microsoft is building on top of the Mono platform's handy Linker tool to turn its code-shrinking powers loose on reducing the size of .NET Core apps.
Mono -- an open source implementation of Microsoft's .NET Framework -- has an IL Linker for slimming down code projects.
"The linker is a tool one can use to only ship the minimal possible IL code and metadata that a set of programs might require to run as opposed to the full libraries," the project's GitHub site says. "It is used by the various Xamarin products to extract only the bits of code that are needed to run an application on Android, iOS and other platforms."
That has been modified and now also exists on GitHub as the .NET IL Linker.
"The .NET team has built a linker to reduce the size of .NET Core applications," the project states. "It is built on top of the excellent and battle-tested mono linker. The Xamarin tools also use this linker.
"In trivial cases, the linker can reduce the size of applications by 50 percent. The size wins may be more favorable or more moderate for larger applications. The linker removes code in your application and dependent libraries that are not reached by any code paths. It is effectively an application-specific dead code analysis."
Microsoft's Scott Hanselman put the tool through its paces in updating a 15-year-old .NET 1.1 application and realized different amounts of code shrinkage.
"You can see in some places where there's no size decrease," Hanselman said. "That's because I'm using those assemblies completely. Some see a 100 percent decrease -- they've been removed entirely -- because I'm not using the Registry, for example. And some see a fractional decrease because I'm using some methods but not others."
Microsoft has listed the following caveats for using the experimental tool:
- Currently only supports publishing self-contained applications. It will fail unless you specify a runtime ID.
- It is currently an experimental feature. We intend to productize it in a subsequent .NET Core release.
- Linking only happens during publish, and therefore the linked app needs to be tested after publish, not just after build.
- The linker can and will break some apps that use reflection. See Using IL Linker Advanced Features for more information about managing reflection usage.
The new experimental tool was released to the community to open it up for experimentation and solicit feedback to improve it. Specifically, Microsoft said it's looking for feedback on topics such as:
- Linker throughput.
- Cases where the linker can be more aggressive.
- Cases where the link is too aggressive and causes applications to fail at runtime.
- The linker provided an excellent result for a large application
Feedback can be provided by opening issues at either mono/linker or dotnet/core or via e-mail to firstname.lastname@example.org.
David Ramel is the editor of Visual Studio Magazine.