How to achieve smaller size of the executable?
Asked Answered
Y

8

14

Very recently I have come back to Delphi after a long pause and written a rather straightforward utility app my client requested to support an older release...

I know these days the size does not matter much, but it struck me as odd that the one-unit application, when compiled, amounted to 1'084'416 b executable. The one and only .pas unit I wrote is some 20.8k large, mostly because of the richness of the gui.

The uses clause is as follows:

uses
  Windows, Messages, SysUtils, Variants, Classes, Controls, Forms, strutils,
  Dialogs, ADODB, DB, DBGrids, ExtCtrls, DBCtrls, StdCtrls, Grids, Menus,
  Buttons;

I wonder if there's any way I could reduce the size of the application to 300-400k or less?

Yamamoto answered 27/1, 2010 at 20:31 Comment(2)
@ChaosPandion: Storage isn't the only consideration here. Among other things, larger files mean larger download times, which affects end-user perception of the program's quality.Aqueduct
Mason Wheeler: It also enforces the users feeling it is just an util instead of an app.Louettalough
G
17
  1. Did you do a debug or release build? (Make it release for smaller size, make sure optimization is on, debug information turned off)

  2. Did you turn off RTII (delphi 2010 and up) if not needed? (Smaller EXE sizes.)

  3. Number of units in your uses clause of your main unit, is not a good way to guess EXE size. Think of it this way: The VCL itself is one large amount of code, the Database Layer another, and the stuff you write is probably a very small percentage of the EXE size.

  4. To understand your executable size, try the JCL Project Analyzer, or read the MAP file that is produced when you turn on the Map option. This will tell you exactly what is inside your executable file.

It would be silly for various reasons, but you could get a smaller executable by using Delphi 7, for example. In the end when I make an application and I want to make it smaller, I look at how much time it would take, and how much effort to rebuild everything (such as with a vcl alternative) and I then say to myself, forget about it.

Georganngeorge answered 27/1, 2010 at 21:44 Comment(3)
Removing unused units can bring down exe size. Specially if they have init code (that often makes code with RTTI (also non D2010) reachable). But only if the unit is not used somewhere else in the program, including 3rd party units that you use. Hygiene in your uses clause is a good practice though.Louettalough
Just select "Release" instead of "Debug" and file from 13mb will be compressed to 2mb !!!!!!Exaggeration
My answer already said that when I wrote it six years ago. Why say it again?Georganngeorge
B
13
  • You can try using KOL (Key Objects Library is a set of objects to develop power (but small) 32 bit Windows GUI applications using Delphi but without VCL). KOL allows to create very compact Windows32 GUI applications (starting from ~11K without compression - if suggested system units replacement used). The most of code is converted to built-in assembler.

  • Another option is use a exe compressor like UPX.

Busk answered 27/1, 2010 at 20:46 Comment(5)
Also see #354134Resupinate
Wouldn't use UPX unless you want to have customers calling about your programs turning up in antivirus warnings all day.Louettalough
@Marco, I know about the existence of some isolated cases of false positives caused by the use of UPX, however I believe that using UPX is still an valid option.Busk
@RRUZ: we gave it up, more trouble than gains. But if you e.g. deliver to desktops of an own company over ISDN or so, and only have to deal with one avirus, (the company wide one), I guess it is survivable and worth the effort.Louettalough
Although UPX reduces exe size it increases the memory needed for you executable.Sepulture
G
8

MapFileStats (DelphiTools.info) is a good (free) tool that allows you to see how much space every unit occupies in your executable. My own tool DelphiUnitSizes is an alternative that in addition to unit sizes also display the size of each function or class.

Delphi 2010 made the default executable about 30% larger, probably because of RTTI included in the RTL/VCL units, so you can use an older version of Delphi for smaller exe-size.

As others have mentioned UPX is a great tool too, the false positives by virusscanners are not that frequent in my experience.

The size of Delphi-executables can be very much trimmed down using custom System-units and UPX-compression. I can generate exe-files that are less that 64kb in size with my Delphi-based game-generator ZGameEditor, even with Delphi Berlin.

Gag answered 28/1, 2010 at 8:18 Comment(0)
L
6

How big is your DFM? It is included as a resource in your EXE. Depending on how complex your GUI is, you might find that creating the GUI at runtime in code could reduce the EXE size.

Leaves answered 27/1, 2010 at 21:42 Comment(2)
(as well as unused properties, runtime instantiating makes the names of the components not necessary, saving some strings)Louettalough
the DFM is 17'103 b. forgot to mention that.Perdue
A
3

You may also considere to add the following line to the top of the project file:

{$SetPEFlags 1}

Explanation here : http://hallvards.blogspot.fr/2006/09/hack12-create-smaller-exe-files.html

Assibilate answered 14/12, 2012 at 14:54 Comment(0)
E
2

Yes, but then you'd need to supply the other code units as additional files. Just as .net required the assembly, and you have VB runtimes etc., this is just the Delphi runtime - but it's embedded in the exe.

Another option is to compress the executable, there are tools for that around.

Eduction answered 27/1, 2010 at 20:35 Comment(4)
Could you suggest a free tool for the second option?Perdue
UPX is an open-source packer.Universality
Others were faster than me, UPX is a well-known one.Eduction
thanks for that upx alternative, I never searched in that direction. Seems to be a partial solution as it reduced the size to somewhat over 300k.Perdue
S
1
  • You say you're coming back to Delphi. If you still have an old version available, use that - every new version adds extra features and if you don't need them them your exes will be smaller without them.

  • Make sure you're only including units you actually use.

But whatever you do I very much doubt you'll get down to 300k. If memory serves, even a 'hello world' application in Delphi 2 would be larger than that.

Staphylorrhaphy answered 29/1, 2010 at 19:22 Comment(0)
F
0

Do you have any resource files or pictures that are linked into the project?

I think the ADODB also includes quite some overhead. If your app really uses a database then a mere 1MB isn't too bad for file size? Don't forget that your app is just this exe - no need for extra dll's etc.

Fibrin answered 28/1, 2010 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.