views:

726

answers:

7

Hello everyone!

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?

+1  A: 

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.

Lucero
Could you suggest a free tool for the second option?
Peter Perháč
UPX is an open-source packer.
vcldeveloper
Others were faster than me, UPX is a well-known one.
Lucero
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.
Peter Perháč
+9  A: 

@MasterPeter, 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.

alt text

Another option is use a exe compressor like UPX

alt text

RRUZ
Also see http://stackoverflow.com/questions/353634/are-there-any-downsides-to-using-upx-to-compress-a-windows-executable
TOndrej
Wouldn't use UPX unless you want to have customers calling about your programs turning up in antivirus warnings all day.
Marco van de Voort
@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.
RRUZ
see also: http://www.aspack.com
Joe Meyer
@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.
Marco van de Voort
Although UPX reduces exe size it increases the memory needed for you executable.
Remko
+4  A: 

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.

Jim McKeeth
(as well as unused properties, runtime instantiating makes the names of the components not necessary, saving some strings)
Marco van de Voort
the DFM is 17'103 b. forgot to mention that.
Peter Perháč
+6  A: 
  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) 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.

Warren P
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.
Marco van de Voort
+3  A: 

MapFileStats (DelphiTools.info) is a good (free) tool that allows you to see how much space every unit occupies in your executable.

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 2010.

Ville Krumlinde
A: 

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.

birger
A: 
  • 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.

Mike Sutton