Create Smaller Delphi XE Executables: Remove RTTI, Pack EXE

Create Smaller Delphi XE Executables: Remove RTTI, Pack EXE

By , About.com GuideJuly 26, 2011

Follow me on:

in RTTI :: Some time ago I've moved my Delphi 2007 project to Delphi XE. The size of the EXE produced by Delphi 2007 was ~ 5 MB (included some resources, no build with packages used).

Moving to Delphi XE resulted in EXE size growth to 10.5 MB!

Ok, "today" the size of the EXE should not be something your users would worry about - but I needed to send beta/test copies to a partner every day / two days / very often.

The RTTI has changed from Delphi 2007. Delphi 2010 and Delphi XE have an "extended RTTI" - making executables larger.

Anyway, I had to shrink the size of my EXEcutable, and since I was not using any RTTI in that project, here's what I did:

program MyDelphiXE

{$WEAKLINKRTTI ON}
{$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])}

...
This was placed under the "program" keyword in the DPR ( Delphi project file).

The "{$WEAKLINKRTTI ON}" compiler directive (default value if OFF) only affects linking - methods are not included in the binary so that RTTI can not find and invoke methods at run time.

The "{$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])}" controls the amount of extended RTTI information that is emitted for a class or record type - it disables all the RTTI options and sets to not emit any extended RTTI information.

With the above directive in place my EXE size was reduced to 9 MB. Not much.

Then, I've found UPX - packer for executables. Using "UPX -9 MyDelphiXE.exe" I've reduced the EXE size to a fantastic 3.5 MB!

Comments
July 26, 2011 at 10:04 am
(1) Erik Knowles says:

UPX packing is almost never worth the trouble — virus scanners *will* give false positives on your executable, exception stack traces won’t work right, etc. See the link below for a nice back-and-forth:

http://stackoverflow.com/questions/353634/are-there-any-downsides-to-using-upx-to-compress-a-windows-executable

July 26, 2011 at 10:14 am
(2) Zarko Gajic says:

@Erik: Thank you for the info. Am using UPX for more than 2 years – have had no issues at all.

July 26, 2011 at 2:07 pm
(3) Wouter van Nifterick says:

You can integrate UPX into the IDE like this:

Go to
/tools/configure tools/

Press “Add…”

Title: Compress Exe // this name will appear under the “tools menu”
Program: C:\bin\upx.exe // or whereever your upx.exe resides
Working directory: // leave it empty
Parameters: -9 $EXENAME // -9 means best compression. It’s also the slowest

July 27, 2011 at 1:10 am
(4) T Jackson says:

I agree with Erik. Using exe compression causes some virus programs using deep heuristic analysis to give a false positive.


July 27, 2011 at 2:14 am
(5) John says:

Antivirus heuristics will also give false positive on uncompressed EXEs, the UPX compression format is open-source, every antivirus worth its salt supports it.

If an antivirus doesn’t, then you should consider switching to another antivirus, because if they couldn’t be bothered with supporting a widespread, open-source compression format, the antivirus is maintained by incompetents that are pretty much guaranteed to be just as incompetent at identifying actual viral threats (for which they won’t have the source)

July 27, 2011 at 2:25 am
(6) marcel says:

almost every delphi version makes the executable a bit bigger i noticed. I use UPX for about 10 years now and never had any problems besides the heuristic analysis thing.

@Wouter van Nifterick, adding –lzma makes it even smaller (for me 10%)

July 27, 2011 at 4:40 am
(7) FloGandhi says:

Wow !! 10.5 MO !!! Delphi 2007…5MB !!! My old good Borland delphi 7 give me a 360Ko.
The ratio is about 20 times. Is it 20 times faster ? Is it 20 time more intuitive or beautiful?

Zarko, please tell them, you where there too. Seriously, I can read the newest cool things add during the evolution of each version of Delphi. As a programmer, something sound wrong, no?

July 27, 2011 at 5:55 am
(8) Zarko Gajic says:

@FloGandhi, yes each new Delphi produces larger EXEs. But, I would not care too much as the feature set also increases. No need for RTTI, just turn it off. For the same application Delphi 7 was also 5MB for me.

July 28, 2011 at 11:13 am
(9) Alex says:

Nice article! I always used the Relocation section remover and had never problems. Together its even better.

July 28, 2011 at 3:37 pm
(10) Ali says:

i had some big programs that had processing works (data processing, memory copying, …)
packing my exe with upx caused my program to terminate suddenly during running

for small programs maybe it is good.

July 30, 2011 at 4:50 am
(11) nicke85 says:

All the greatings from Serbia and thany for info..
Why you use upx with -9 options when
upx –best project.exe is the best compression (10 level)

August 2, 2011 at 1:22 pm
(12) Kevin G. McCoy says:

I am the IT Manager in charge of a corporate LAN.

Our firewall box blocks any incoming emails with attached UPX-compressed EXE files. It catches 2-3 per day, and yes, they are all viruses (not false-positives) if I check them manually.

Virus writers love UPX, so I would not use it in a corporate environment, if I were you.

August 30, 2011 at 12:38 am
(13) Søren B. says:

@Kevin look at John’s post (5) – it’s sad when someone judges a book by its cover. Be it AntiVir SW or a firewall box.
Don’t you think your (corporate) firewall catches them because of the content – not the wrapping?

Gangsters love beautiful women, so you’d rather avoid beautiful women?

February 7, 2012 at 11:21 am
(14) Enzo says:

Hi,
when I use UPX compressor the size of my application from 32 Mb goes to 9 Mb!!!
But I’ve noted that when mi application is running the occupation of RAM goes from 25 MB up to 75 MB.
Have you an Idea?
Thanks
Enzo

March 14, 2012 at 3:51 pm
(15) David Heffernan says:

Using UPX just means that your app needs a lot more RAM to run and starts up slower. I can’t imagine many situations where this would be a good idea.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值