Reduce your .NET assembly size
12 April 2007 in .Net, Code | Comments enabled

I’ve always been interested in writing very small and efficient code. There isn’t much need for coding in C or Assembler these days (which I used to like playing with a few years back) so I really enjoy finding new ways of making .NET code smaller and faster. I thought I would share one inexpensive trick for making your assemblies just that bit smaller on disk.

  • Bring up the properties to your C# project file.
  • Click on the build tab.
  • Click on Advanced.
  • Set the File Alignment value to 512 (it will default to 4096 for C# projects)

File Alignment Property on C# Projects Dialog

For some reason Visual Basic solutions will default to 512 and C# projects seem to default to 4096. This affects how the MSIL code is written in the assembly and appropriately pads the file with zeros if required to fit code neatly into the file alignment size. Reducing this value has negligible or no affect on application start-up but can reduce your assembly size by a noticeable amount. Likely in the 10 – 20% range in terms of savings.

Always nice to get a free saving on assembly size!

- JD


1 comment. Add your own comment.

Huff says 18 November 2008 @ 14:52

Just got bit by this one. Thanks for posting this, would have blown my brains out trying to figure why my msbuild assemblies were bigger than my visual studio ones.

Leave a Comment

Name (required)

E-mail (required - not published)

Website

Your comment: