[SOLVED] Using Numpy.NET NuGet package in C# class library making .exe file too big

Issue

I’m using Numpy.NET NuGet package in C# class library and it’s making .exe files on release build too big (from ~10 to ~30 MB). What is the exact reason for that and is there any solution to this problem?

I used ILDASM to get stats of .exe file and this is what it showed:

 File size            : 31318016
 PE header size       : 512 (472 used)    ( 0.00%)
 PE additional info   : 366928            ( 1.17%)
 Num.of PE sections   : 2
 CLR header size     : 72                 ( 0.00%)
 CLR meta-data size  : 157972             ( 0.50%)
 CLR additional info : 30706176           (-39.09%)
 CLR method headers  : 8741               ( 0.03%)
 Managed code         : 77296             ( 0.25%)
 Data                 : 367104            ( 1.17%)
 Unaccounted          : -366785           (-1.17%)

 Num.of PE sections   : 2
   .text    - 30950400
   .rsrc    - 367104

 CLR meta-data size  : 157972
   Module        -    1 (10 bytes)
   TypeDef       -  275 (3850 bytes)    8 interfaces, 0 explicit layout
   TypeRef       -  436 (2616 bytes)
   MethodDef     - 1598 (22372 bytes)   29 abstract, 0 native, 1537 bodies
   FieldDef      - 1013 (6078 bytes)    20 constant
   MemberRef     - 1210 (7260 bytes)
   ParamDef      - 1212 (7272 bytes)
   MethodImpl    -   65 (390 bytes)
   Constant      -  189 (1134 bytes)
   CustomAttribute- 1041 (6246 bytes)
   StandAloneSig -  210 (420 bytes)
   InterfaceImpl -   77 (308 bytes)
   PropertyMap   -  104 (416 bytes)
   Property      -  400 (2400 bytes)
   MethodSemantic-  731 (4386 bytes)
   TypeSpec      -  255 (510 bytes)
   Assembly      -    1 (22 bytes)
   AssemblyRef   -   27 (540 bytes)
   ManifestResource-   48 (576 bytes)
   NestedClass   -   68 (272 bytes)
   EventMap      -   19 (76 bytes)
   Event         -   28 (168 bytes)
   GenericParam  -   12 (96 bytes)
   MethodSpec    -  173 (692 bytes)
   GenericParamConstraint-    1 (4 bytes)
   Strings       - 50364 bytes
   Blobs         - 18320 bytes
   UserStrings   - 20924 bytes
   Guids         -    16 bytes
   Uncategorized -   234 bytes

 CLR additional info : 30706176
   Resources         - 30706176

 CLR method headers : 8741
   Num.of method bodies  - 1537
   Num.of fat headers    - 413
   Num.of tiny headers   - 1124
   Num.of fat sections   - 14
   Num.of small sections - 81

 Managed code : 77296
   Ave method size - 50

Solution

Turns out the issue was in .csproj file. It has following lines, that embeds all .dll’s on "AfterResolveReferences" event (don’t know if it’s VS default settings or previous dev’s did this on purpouse).

<Target Name="AfterResolveReferences">
    <ItemGroup>
      <EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">
        <LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
      </EmbeddedResource>
    </ItemGroup>
</Target>

My sln have multiple projects and main project doesn’t need all libraries that this solutions use, so i added condition to exclude all python-related libraries and it did the trick. This didn’t hurt the programm, because code library project that use Numpy.net still working great after installer build and .exe file now 20 MB lighter.
Below the modified code:

<Target Name="AfterResolveReferences">
    <ItemGroup>
      <EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll' And '%(ReferenceCopyLocalPaths.Filename)' != 'Numpy' And '%(ReferenceCopyLocalPaths.Filename)' != 'Python.Runtime' And '%(ReferenceCopyLocalPaths.Filename)' != 'Python.Included'
        And '%(ReferenceCopyLocalPaths.Filename)' != 'Python.Deployment'">
        <LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
      </EmbeddedResource>
    </ItemGroup>
</Target>

Answered By – Nikolai Naberezhnev

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *