Microsoft linker tool shrinks .Net applications
A long-requested and long-unfulfilled feature for .Net has finally been delivered by Microsoft and the Mono team: A linker that allows .Net applications to be stripped down to include only the parts of libraries that are actually used by the program at runtime.
The IL Linker project works by analyzing a .Net application and determining which libraries are never called by the application in question. “It is effectively an application-specific dead code analysis,” says Microsoft in its GitHub announcement for the project.
A long-term mission for IL Linker is to make it into “the primary linker for the .Net ecosystem.”
The current, preview version of IL Linker only supports a small subset of possible .Net applications—those that are self-contained and use the .Net Core. How much space is saved for a given application will vary widely, but Microsoft states that “in trivial cases, the linker can reduce the size of applications by 50%.”
It has historically been difficult to slim down .Net applications because of the platform’s dynamic features. IL Linker searches through the code starting from any assemblies or dependencies that are explicitly declared in the application, and flags everything that it determines to be unused.
Right now, the algorithms used for this mark-and-sweep err on the side of caution, but it is still possible for IL Linker to flag code that is dynamically imported at runtime. However, the end user can always manually specify what to keep, whether it is an entire assembly or only a specific type within that assembly.
Some of IL Linker’s work is based on an earlier project, Cecil, used by .Net developers since 2004 to inspect and modify .Net apps and libraries. The chief difference between the two is that Cecil is mainly for manual inspection and modification, with the creation of a smaller app just one of several possible goals. IL Linker is designed specifically for slimming down the footprint of a .Net app.