.NET Core 2.0应用程序减少体积瘦身官方工具 IL Linker。linux
IL Linker 来源于mono的linker https://github.com/mono/linker,目前仍是预览版本。git
在通常的状况下,连接器能够将应用程序的大小减小50%,大型应用程序的大小可能更有利,连接器会删除应用程序中的代码和依赖库,而这些代码不会被任何代码路径访问。它其实是应用程序特定的无效代码分析。github
下面正式开始体验json
版本必须为.NET Core 2.0api
新建一个控制台应用工具
dotnet new console -o linkdemo测试
而后添加nuget.configspa
dotnet new nugetcode
接着在config 中加入 <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" /> 以下:xml
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <!--LineZero --> <clear /> <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" /> </packageSources> </configuration>
添加ILLink 程序包
dotnet add package ILLink.Tasks -v 0.1.4-preview-906439
最新版本能够到https://dotnet.myget.org/feed/dotnet-core/package/nuget/Illink.Tasks 查看
发布程序
dotnet publish -c release -r <RID> -o out
win-x64
, win-x86
, linux-x64
, osx-x64
win10 包含linker的发布
dotnet publish -c release -r win10-x64 -o linker
不包含linker
dotnet publish -c release -r win10-x64 -o nolinker /p:LinkDuringPublish=false
均可以成功执行。
咱们看看体积大小。
基本上减少50%以上,文件减小2/3 。
/p:LinkDuringPublish=false
- 禁用连接器。/p:ShowLinkerSizeComparison=true
- 显示应用程序大小缩小的列表。显示程序缩小列表
dotnet publish -c release -r win10-x64 -o linker /p:ShowLinkerSizeComparison=true
真正意义上的.NET Core 瘦身。
注意事项:
参考文档:https://github.com/dotnet/core/blob/master/samples/linker-instructions.md