Scott Hanselman

Making a tiny .NET Core 3.0 entirely self-contained single executable

June 13, '19 Comments [18] Posted in DotNetCore
Sponsored By

I've always been fascinated by making apps as small as possible, especially in the .NET space. No need to ship any files - or methods - that you don't need, right? I've blogged about optimizations you can make in your Dockerfiles to make your .NET containerized apps small, as well as using the ILLInk.Tasks linker from Mono to "tree trim" your apps to be as small as they can be.

Work is on going, but with .NET Core 3.0 preview 6, ILLink.Tasks is no longer supported and instead the Tree Trimming feature is built into .NET Core directly.

Here is a .NET Core 3.0 Hello World app.

225 files, 69 megs

Now I'll open the csproj and add PublishTrimmed = true.

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
</Project>

And I will compile and publish it for Win-x64, my chosen target.

dotnet publish -r win-x64 -c release

Now it's just 64 files and 28 megs!

64 files, 28 megs

If your app uses reflection you can let the Tree Trimmer know by telling the project system about your Assembly, or even specific Types or Methods you don't want trimmed away.

<ItemGroup>
<TrimmerRootAssembly Include="System.IO.FileSystem" />
</ItemGroup>

The intent in the future is to have .NET be able to create a single small executable that includes everything you need. In my case I'd get "supersmallapp.exe" with no dependencies. That's done using PublishSingleFile along with the RuntimeIdentifier in the csproj like this:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
</Project>

At this point you've got everything expressed in the project file and a simple "dotnet publish -c Release" makes you a single exe!

There's also a cool global utility called Warp that makes things even smaller. This utility, combined with the .NET Core 3.0 SDK's now-built-in Tree Trimmer creates a 13 meg single executable that includes everything it needs to run.

C:\Users\scott\Desktop\SuperSmallApp>dotnet warp
Running Publish...
Running Pack...
Saved binary to "SuperSmallApp.exe"

And the result is just a 13 meg single EXE ready to go on Windows.

A tiny 13 meg .NET Core 3 application

If you want, you can combine this "PublishedTrimmed" object with "PublishReadyToRun" as well and get a small AND fast app.

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
</Project>

These are not just IL (Intermediate Language) assemblies that are JITted (Just in time compiled) on the target machine. These are more "pre-chewed" AOT (Ahead of Time) compiled assemblies with as much native code as possible to speed up your app's startup time. From the blog post:

In terms of compatibility, ReadyToRun images are similar to IL assemblies, with some key differences.

  • IL assemblies contain just IL code. They can run on any runtime that supports the given target framework for that assembly. For example a netstandard2.0 assembly can run on .NET Framework 4.6+ and .NET Core 2.0+, on any supported operating system (Windows, macOS, Linux) and architecture (Intel, ARM, 32-bit, 64-bit).
  • R2R assemblies contain IL and native code. They are compiled for a specific minimum .NET Core runtime version and runtime environment (RID). For example, a netstandard2.0 assembly might be R2R compiled for .NET Core 3.0 and Linux x64. It will only be usable in that or a compatible configuration (like .NET Core 3.1 or .NET Core 5.0, on Linux x64), because it contains native code that is only usable in that runtime environment.

I'll keep exploring .NET Core 3.0, and you can install the SDK here in minutes. It won't mess up any of your existing stuff.


Sponsor: Suffering from a lack of clarity around software bugs? Give your customers the experience they deserve and expect with error monitoring from Raygun.com. Installs in minutes, try it today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Monday, June 17, 2019 7:13:25 AM UTC
Lovely, gone are the times when a single Windows Forms app with one Button that would display a MessageBox with "Hello World" would use a 100 megabytes of memory... :-P
Daniel
Monday, June 17, 2019 7:14:46 AM UTC
.NET core self-contained assemblies is pure magic!
Monday, June 17, 2019 8:47:17 AM UTC
How is this related to the --self-contained paramerer of the dotnet publish command?
Monday, June 17, 2019 3:23:32 PM UTC
Great post - minimizing the output is a wet dream for most developers - I hope :)


I don't think there is any mention of a parameter called --self-contained in the this post; or in the comment section, for that matter.
However :) The term self-contained is used to express what the goal is.
Christian Münster
Monday, June 17, 2019 4:09:13 PM UTC
Long way to go to get down to a 402 bytes hello world exe, but a great step in the right direction. It would be fun to see a multi-platform R2R .exe :)
Michael Paine
Monday, June 17, 2019 8:15:35 PM UTC
Sadly, in the preview6, asp.netcore or blazor app fails to be single exe compiled.
Mihai
Monday, June 17, 2019 11:31:53 PM UTC
I love that 13 MB is a small app now :)

I remember when Netscape Navigator hit 6MB on my dial up 14.4k modem and though it was the most evil thing would leave on overnight to download.
Justin King
Tuesday, June 18, 2019 9:39:36 AM UTC
@Justin King : my thoughts exactly. I seem to remember that the whole Windows 3.1 fit on a bunch of diskettes, that you could hold in one hand. 5 or 6 or so. I agree that the reduction in size within this article is a lot, but 13 megabytes for "hello world", still makes me gringe worse that I would like to :-D
El Dorko
Tuesday, June 18, 2019 12:11:49 PM UTC
Truth be told, a lot of these newfangled compiled languages produce similar sized executables when compiling an "Hello, World!"
MaxL
Tuesday, June 18, 2019 4:49:04 PM UTC
This looks great, but is doesn't seem to copy any assets across (images for example).
Paul Michaels
Tuesday, June 18, 2019 6:00:51 PM UTC
Just to remember, compressed executable files make virtual memory manager to work a little harder - decompressed data has to be written to disk to free up the memory instead of simply discarding unused data blocks and reloading them from the executable image if needed again.
Dmitry Slobodyanyuk
Wednesday, June 19, 2019 5:18:23 PM UTC
@Daniel - I just created a WinForms app with three buttons and a rich text box. 4.3MB in memory on x86. Did you mean 10MB and not 100MB?
Chris
Thursday, June 20, 2019 11:29:40 AM UTC
Hi Scott,
Nice blog post :-)

Is there a way(or will there be in the future) to create a "self-contained single executable" for a asp.net core or Blazor (ASP.NET Core hosted) app ?
Boe Mark
Friday, June 21, 2019 3:44:01 PM UTC
13 meg.. sounds like an achievement. What are you trying to push here?

Still no answers about the HUGE security problems you have when distribute .net core apps.
Edward
Friday, June 21, 2019 4:33:44 PM UTC
This doesn't work on WPF. I've added

PublishReadyToRun>true</PublishReadyToRun>


and published with

dotnet publish -r win-x64 -c release


but the size is pretty 100% identical. Moreover, there is no performance improvements at all. In your example it shows

<OutputType>Exe</OutputType>


but shouldn't it be

<OutputType>WinExe</OutputType>


as it is for WPF? What kind of application did you use? Winforms? WPF? Console only? I also couldn't find any documentation about this, whether it supports WPF or just winforms or just console only or whether it has been tested with WPF at all, seems like not.

Using .NET Core 3 Preview 7.
Bob
Monday, June 24, 2019 12:07:39 PM UTC
"Still no answers about the HUGE security problems you have when distribute .net core apps."

Edward, can you elaborate on the security problems you're referring to?
Thomas Clarke
Tuesday, June 25, 2019 8:43:14 AM UTC
I would be more concerned about all of the libraries being in the same directory as the executable, whether you have 75 or 17 DLLs in the same directory as the executable the user has to click on, they will find it intimidating. Any word on being able to simply put the DLLs in a separate folder?
Ivan
Tuesday, June 25, 2019 4:59:17 PM UTC
I'm just very curious to know if there are any plans to allow these types of builds to be easily distributable without the hosting bundle for IIS integration?

Currently, the ASP.NET Core Hosting Module is only offered via the hosting bundle that includes both x32 and x64 versions of the respective run time, which from a software distribution standpoint kills the benefit of the self-contained and/or trimmed builds.

Don't get me wrong, this is all well and good for command line/desktop applications but still leaves the ASP.NET Core side of the fence out to dry because of how MS offers .NET Core downloads.
Jason Mock
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.