也许这篇Tips有点用:
不要用太多的Form,可能吗?
Reduce the .EXE-size of your app
Use CASE.. statments rather than IF..ELSE.. clauses.
In every .PAS-file that will be linked in your project, place the following line to the
top of your code:
{$D-,L-,O+,Q-,R-,Y-,S-}
Also add this line to your project source (.DPR).
{$D-} will prevent placing Debug info to your code.
{$L-} will prevent placing local symbols to your code.
{$O+} will optimize your code, remove unnecessary variables etc.
{$Q-} removes code for Integer overflow-checking.
{$R-} removes code for range checking of strings, arrays etc.
{$S-} removes code for stack-checking. USE ONLY AFTER HEAVY TESTING !
{$Y-} will prevent placing smybol information to your code.
After doing this, recompile the whole project - your .EXE-size should magically have
been reduced by 10-20 %..
If you link in graphics, they don't need to have more than 16 or 256 colors.
If the goal is really and only .EXE size, load your graphics by code ("manually").
If you include resource-files, they should only content the resources you really need
- and nothing more.
If you use Delphi 1, finally run W8LOSS.EXE on your .EXE file (not required for
32bit-Apps).
Delphi 2 produces larger .EXE sizes than Delphi 1 - 32 bit code demands its tribute..
Delphi 3 produces larger .EXE sizes than Delphi 2 - main reason: market pressure
leads to more "fundamental" support of "bells and whistles" - quality and
usefulness/efficiency/productivity doesn't seem to be a real criteria in M$-times...
check the "Show Hints" and "Show Warnings" options on the
Project|Options|Compiler page, then rebuild your project. It will show you every
variable and proc/func that isn't being used. You might be able to trim a little there,
too.
Clean your USES.. clauses for all unneeded units !
The so-called "SmartLinking" doesn't always remove all unused code. In a large
project you could possibly spare 100k in the EXE by that.
Place Bitmaps/Glyphs/etc. in DLL's instead of in *.RES/*.DCR files !
If you place fx some large bitmaps in a .RES, these will compiled into the EXE.
Remove the .RES-declarations, instead place them in a new project like this:
LIBRARY My_Extern_RESes;
USES EmptyUnit;
{$R MyRes1.RES}
{$R MyRes2.RES}
...
INITIALIZATION
END.
The unit AEmptyUnit is a completely empty unit. You have to use a unit like this
because any other unit will place unnecessary code in the final DLL. When you want
to use an image (and typically, you only want to load it once), you can do it like this
:
MyHandle := LoadLibrary('My_Extern_RESes.DLL');
TButton1.Glyph.Handle := LoadBitmap(MyHandle,'My_Glyph');
Placing Glyphs, Bitmaps, String-const's etc. in DLL's can really reduce a projects
EXE-size. (Tip came from David Konrad)
Basis rule: the more forms and units you add, the larger your exe becomes. When
you had one huge form, you only had one form class - even when you had 500+
things on it ! This creates only one runtime type information for that class. Now,
when you split this into 17 units, each class in that unit requires its own RTTI. Now,
this runtime information can get large, collectively, with a lot of redundant
information compared to when you only had one huge class. Also, if each of the 17
units had its own form, your end product must contain resource information for all 17
of those forms even though most of that information may be redundant. One reason
why this happens is because the Delphi compiler doesn't optimize resources - but I
don't know of any compiler that does. So, if you had two separate forms which are
identical in look, you'll have 2 copies of the resource in your .EXE.
This leaves some work for the programmer to be creative in maximizing resource
reuseability. (Tip came from Young Chung)