.NET8
根据官方更新:截至到2023年2月23日, .NET 8 Preview 1 AOT编译 在大小方面有重大进步。
.NET 7 | .NET 8 Preview 1 | |
---|---|---|
Linux x64 (with -p:StripSymbols=true) | 3.76 MB | 1.84 MB |
Windows x64 | 2.85 MB | 1.77 MB |
实际测试:
PS D:\Project\net8> dotnet publish -r win-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true -p:StripSymbols=true --self-contained true
MSBuild version 17.6.0-preview-23108-10+51df47643 for .NET
正在确定要还原的项目…
所有项目均是最新的,无法还原。
C:\Program Files\dotnet\sdk\8.0.100-preview.1.23115.2\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInf
erence.targets(287,5): message NETSDK1057: 你正在使用 .NET 的预览版。请参阅 https://aka.ms/dotnet-support-policy [D:\Project\net8\ne
t8.csproj]
net8 -> D:\Project\net8\bin\Release\net8.0\win-x64\net8.dll
net8 -> D:\Project\net8\bin\Release\net8.0\win-x64\publish\
PS D:\Project\net8> ls -l .\bin\Release\net8.0\win-x64\publish\
Directory: D:\Project\net8\bin\Release\net8.0\win-x64\publish
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2023/2/23/周四 10:31 11698126 net8.exe
-a--- 2023/2/23/周四 10:29 10212 net8.pdb
结果变化很小,即便是加上 -p:StripSymbols=true
参数!!
原文参考:https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-1/#native-aot
.NET6 和 .NET7
半年前(2月20日)测试过,现在(2021年8月25日)再次使用最新的 .NET 6.0.0-preview.7 再次验证一下。
PS C:\Users\Administrator> dotnet --version
6.0.100-preview.7.21379.14
直接新建了一个默认项目:
PS D:\Project\net6> dotnet new console
The template "Console Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on D:\Project\net6\net6.csproj...
正在确定要还原的项目…
已还原 D:\Project\net6\net6.csproj (用时 75 ms)。
Restore succeeded.
默认单文件,一个Hello World! 60MB 60.2MB:
PS D:\Project\net6> dotnet publish -r win-x64 -p:PublishSingleFile=true --self-contained true
用于 .NET 的 Microsoft (R) 生成引擎版本 16.9.0-preview-21103-02+198f3f262
版权所有(C) Microsoft Corporation。保留所有权利。
正在确定要还原的项目…
所有项目均是最新的,无法还原。
你正在使用 .NET 的预览版。请查看 https://aka.ms/dotnet-core-preview
net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\net6.dll
net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\
PS C:\Users\Administrator> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\net6.exe
Hello World!
PS D:\Project\net6> dir .\bin\Debug\net6.0\win-x64\publish\
目录: D:\Project\net6\bin\Debug\net6.0\win-x64\publish
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2021/2/3 星期三 4:11 1304944 mscordaccore.dll
-a---- 2021/2/20 星期六 11:09 60808321 net6.exe
-a---- 2021/2/20 星期六 11:05 9404 net6.pdb
使用PublishTrimmed
和PublishReadyToRun
参数后 21MB 13.6MB。
PS D:\Project\net6> dotnet publish -r win-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true -p:PublishReadyToRun=true --self-contained true
用于 .NET 的 Microsoft (R) 生成引擎版本 16.9.0-preview-21103-02+198f3f262
版权所有(C) Microsoft Corporation。保留所有权利。
正在确定要还原的项目…
所有项目均是最新的,无法还原。
你正在使用 .NET 的预览版。请查看 https://aka.ms/dotnet-core-preview
net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\net6.dll
正在优化程序集的大小,这可能更改应用的行为。请务必在发布后进行测试。请参阅: https://aka.ms/dotnet-illink
net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\
PS D:\Project\net6> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\net6.exe
Hello World!
PS D:\Project\net6> dir .\bin\Debug\net6.0\win-x64\publish\
目录: D:\Project\net6\bin\Debug\net6.0\win-x64\publish
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2021/2/3 星期三 4:11 1304944 mscordaccore.dll
-a---- 2021/2/20 星期六 11:18 21196983 net6.exe
-a---- 2021/2/20 星期六 11:05 9404 net6.pdb
只使用PublishTrimmed
参数,20MB 10.8MB。
PS D:\Project\net6> dotnet publish -r win-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true
用于 .NET 的 Microsoft (R) 生成引擎版本 16.9.0-preview-21103-02+198f3f262
版权所有(C) Microsoft Corporation。保留所有权利。
正在确定要还原的项目…
所有项目均是最新的,无法还原。
你正在使用 .NET 的预览版。请查看 https://aka.ms/dotnet-core-preview
net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\net6.dll
正在优化程序集的大小,这可能更改应用的行为。请务必在发布后进行测试。请参阅: https://aka.ms/dotnet-illink
net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\
PS D:\Project\net6> dir .\bin\Debug\net6.0\win-x64\publish\
目录: D:\Project\net6\bin\Debug\net6.0\win-x64\publish
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2021/2/3 星期三 4:11 1304944 mscordaccore.dll
-a---- 2021/2/20 星期六 11:22 19057335 net6.exe
-a---- 2021/2/20 星期六 11:05 9404 net6.pdb
PS D:\Project\net6> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\net6.exe
Hello World!
PS D:\Project\net6>
最后:.NET6的重点当然不是 单文件与go已经没啥差别了。PublishSingleFile
,如果是小工具还是go有优势。
参考:https://docs.microsoft.com/zh-cn/dotnet/core/deploying/single-file