我可以做什么来减少我的可执行文件的大小(Delphi)?

发布于 2024-09-08 06:26:03 字数 1692 浏览 7 评论 0原文

我使用 Delphi 2009 为桌面程序发布了单个可执行文件 (.EXE)。我没有程序运行所需的外部 DLL 或资源。

我使用两个组件: LMD Innovative 的 ELPack Sergey Tkachenko 的 TRichView 已编译到我的可执行文件中。

当我使用“Release”构建配置构建生产版本时,生成的可执行文件为 13,533 KB。

在使用 Delphi 2009 之前,我使用的是 Delphi 4。它生成的可执行文件只有 2,671 KB,同时包含相同的两个组件,并且基本上具有与我当前版本相同的代码。

我确实知道 Delphi 2009 完全是 Unicode(这是我升级的主要原因),并且 Unicode 可能会导致大小增加一倍。但这大约是原来的5倍。

我的可执行文件必须保持 5 倍大,是否有原因?或者是否有一些简单的方法可以减少大部分可执行文件的大小?


请注意。有些人正在回答压缩Delphi EXE的方法。这不是我想做的。我试图简单地了解为什么要使用如此多的空间来删除​​可能不需要的内容。如果这样做了,如果需要的话,之后仍然可以进行压缩。

安装后,可执行文件的大小实际上并不重要。它用于下载目的并最大限度地减少您想要压缩的服务器负载和下载时间。我更喜欢使用 Inno Setup 并将程序压缩在安装例程本身内。然后在安装时,它会扩展为完整尺寸。这既可以防止可能被检测为病毒,又可以消除在内存中解压缩程序所需的额外启动时间。此外,我对可执行文件和安装例程进行了代码签名,并且某些压缩技术与之不兼容。

有关压缩的更多信息,请参阅 StackOverflow 问题:Delphi EXE 压缩器?


ldsandon 要求我提供我正在使用什么选项,所以它们是:

编译选项
(来源:beholdgenealogy.com

链接选项
(来源:beholdgenealogy.com

I release a single executable (.EXE) for a desktop program using Delphi 2009. I have no external DLLs or resources that I need for the program to run.

I use two components: LMD Innovative's ELPack and Sergey Tkachenko's TRichView that are compiled into my executable.

When I build my production version, using the "Release" build configuration, the executable file produced is 13,533 KB.

Prior to using Delphi 2009, I was using Delphi 4. The executable it produced was only 2,671 KB while incorporating the same two components and basically having the same code as my current version.

I do understand that Delphi 2009 is completely Unicode (which is the main reason why I upgraded), and being Unicode can cause up to a doubling of size. But this is about 5 times larger.

Is there a reason why my executable has to remain 5 times larger? Or are there some simple ways to cut down a significant chunk of the executable size?


Please note. Some people are answering with ways to compress the Delphi EXE. That is not what I am trying to do. I am trying to simply see why so much space is being used to remove what might not be necessary. If that is done, compression can still be done afterwards if so desired.

It really doesn't matter how big or small the executable is once it is installed. It is for downloading purposes and to minimize server load and download times that you want to compress it. I prefer to use Inno Setup and compress the program inside the install routine itself. Then when it is installed, it is expanded to full size. That both prevents possible detection as a virus and eliminates the extra startup time needed to uncompress the program in memory. Also I code sign both my executable and my install routine and some compression techniques are incompatible with that.

For more info about compressing, see the StackOverflow question: Delphi EXE compressor?


ldsandon asked me to provide exactly what options I'm using, so here they are:

Compiling Options
(source: beholdgenealogy.com)

Linking Options
(source: beholdgenealogy.com)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

草莓酥 2024-09-15 06:26:03

例如,从 Delphi 7 迁移到 Delphi 2010 时,我们的 .exe 从 16 兆增长到 35 兆。

几周前我在 Embarcadero 论坛上问了一个与您类似的问题。 (链接)在我的OP中,我列出了一系列链接您可能会发现这个主题有帮助。

我们尝试使用 UPX 来压缩 .exe。让它工作几个小时会显着减少我们的 .exe,但由于以下原因,我们可能不会在生产中使用它:

  1. 我们有相当多的 .exe,并且不想在每个构建上等待 1/2 天。 (我们可能可以找到 UPX 的非暴力参数集来减少这种情况...)

  2. 虽然 .exe 的大小减小了,但我们的可交付却没有,因为我们的安装程序(毫不奇怪)无法从已经压缩的文件中挤出更多压缩...而它能够将原始的 16 meg .exe 减少到 8 megs。

  3. 我读过一些报告,称有时(很少,但并非从来没有),UPX exe 会触发各种防病毒程序来报告应用程序包含病毒。 (我不记得我看到这个的日期、地点或细节,所以我在这里报告它有点不公平。)但是,我们非常不愿意冒这种可能性发生的风险,UPX不可能...

Embarcadero 论坛上的链接还包括 链接到有关此主题的另一个 SO 线程。

我仍然对迁移到 Delphi 2010 时发现的代码膨胀感到惊讶和失望。正如 Nick 指出的,Unicode 的 2X 相当过分。

然而,在迁移到 D2010 时,膨胀只是一个相对较小的权衡,因为在我看来,D2010 在许多其他方面都是一次出色的升级。但是,这确实意味着我们可能不得不改为运送 2 张 CD,而不是一张。我不期待看到我们组织对此的反应......

When moving from Delphi 7 to Delphi 2010., our .exe's grew for example from 16 megs to 35 megs.

I asked a question similar to yours on the Embarcadero forum a few weeks ago. (link) In my OP, I listed a series of links on this subject that you might find helpful.

We tried using UPX to compress our .exe's. Letting it work for hours significantly reduced our .exe, but we probably won't use it in production for these reasons:

  1. We have quite a few .exe's and don't want to wait 1/2-day on each build. (It's possible that we could find a non-brute force set of parameters to UPX that would reduce this...)

  2. Although the size of the .exe is reduced, our shippable was not, because our installer (not surprisingly) is unable squeeze much more compression out of the already compressed file... whereas it was able to reduce the original 16 meg .exe down to 8 megs.

  3. I've read some reports that at some time (rarely, but not never), UPX exe's triggered various anti-virus programs to report the application contained a virus. (I don't recall the date, site, or details of where I saw this, so it's a bit unfair of me to report it here.) But, we are so adverse to taking a risk of that even possibility happening, that UPX is off the table...

The link on the Embarcadero forum also includes a link to another SO thread on this topic.

I continue to be surprised and disappointed at the code bloat we found when moving to Delphi 2010. As Nick notes, 2X for Unicode is quite excessive.

However, the bloat is a relatively minor trade-off when moving to D2010, because, IMO, D2010 is such a terrific upgrade in so many other ways. But, it does mean that we'll probably have to move to shipping 2 CDs rather than one. I'm not looking forward to seeing the reaction to this from our organization...

香草可樂 2024-09-15 06:26:03

如果没有看到“发布”构建配置使用的实际设置,解释这种大小的增加需要大量的猜测。

除了一些可能不太可能的因素导致“拖入”的代码量大幅增加(即使未使用代码)之外,这种增加的幅度最容易通过包含调试信息来解释。

我会检查您的编译器和链接器设置:

  • 调试信息(编译器设置)
  • TD32 信息(链接器)
  • 远程调试信息(链接器)

将 Delphi 2009 项目中的这些设置与 Delphi 4 中的等效设置进行比较。

Without seeing the actual settings that your "Release" build configuration uses explaining this increase in size requires a great deal of speculation.

Beyond some perhaps unlikely factors resulting in a vast increase in the amount of code being "dragged in" even though it isn't used, that magnitude of increase would most easily be explained by the inclusion of debug information.

I would check your compiler and linker settings for:

  • Debug Information (compiler setting)
  • TD32 info (linker)
  • Remote debug info (linker)

Compare these settings in your Delphi 2009 project with the equivalents in Delphi 4.

妄断弥空 2024-09-15 06:26:03

考虑到 Unicode 带来的 2 倍预期增长,您最终会得到 2.5 倍的增长,但未计算在内。考虑到您跳过了多少个版本,这是有道理的。自 Delphi 4 以来,VCL 和 RTL 中添加了很多内容,但并非所有内容都可以轻松智能链接,即使您从未使用过它们。根据您使用的设备数量,您可能会携带相当多的额外行李。

Allen Bauer 和编译器团队在 D2010 中添加了一项新功能来帮助减少这种情况,但显然他们正在谨慎行事,并没有在尽可能多的地方使用它。希望我们能在 2011 年及后续版本中看到更多的垃圾减少。

Factor out the expected 2X increase from Unicode and you end up with a 2.5X increase unaccounted for. This makes sense considering how many versions you've skipped. A lot's been added to the VCL and RTL since Delphi 4, and not all of it is stuff that can be easily smartlinked out, even if you never use it. Depending on how many units you're using, you could be hauling in quite a bit of extra baggage.

Allen Bauer and the compiler team added a new feature into D2010 to help reduce this, but apparently they're treading cautiously and didn't use it in as many places as they could have. Hopefully we'll see more cruft reduction in 2011 and subsequent releases.

白芷 2024-09-15 06:26:03

使用“upx - 压缩或扩展可执行文件”@ http://upx.sourceforge.net


如果您转到工具/配置工具,并像这样进行设置,您可以通过 IDE 中的菜单项轻松压缩您正在处理的可执行文件。

配置

Use "upx - compress or expand executable files" @ http://upx.sourceforge.net


If you go to tools/configure tools, and set it up like this, you can compress the executable that you're working on easily via a menu item in the IDE.

Configuration

独自唱情﹋歌 2024-09-15 06:26:03

我会补充几句话。
仅当链接器能够遵循代码层次结构时,它才可以删除未使用的过程和函数。下面列出了链接器的噩梦列表:

  • 消息驱动的代码,令人遗憾的是,该代码无论如何都无法删除,这就是为什么 Delphi 空白项目大小从一个版本到另一个版本不断增长的原因。每个新的 Windows 消息(例如 WM_TOUCH,据我所知最近引入)都会创建无法删除的过程调用层次结构(即使您根本没有计划使用 Touch API)。这是因为每个 case WM_: 片段都是链接器无法决定是否使用它的东西。

  • 从单元的开始、结束、初始化、终结部分访问代码和数据结构。在这里您可以进行一些控制,删除不必要的调用或对象创建。即使您按需创建对象并仅在终结部分释放它们,也要小心

I will add my few words.
Linker can remove unused procedures and functions only if it can follow the code hierarchy. The nightmare list for linker listed below:

  • Message-driven code, the sad news is that this code can't be removed whatsoever, that's why Delphi blank project size continues growing from version to version. Every new windows message (WM_TOUCH for example as long as I know introduced recently) creates procedure call hierarchy that can't be removed (even if you don't have plan to use Touch API at all). This is because every case WM_: fragment is something linker can't decide whether it will be used or not.

  • Code and data structures accessed from the begin end, initialization, finalization secions of the units. Here you have some control, remove unnecessary calls or object creation. Even if you create objects on demand and only free them in finalization section, make it carefully

美羊羊 2024-09-15 06:26:03

另一种方法是查看“增加尺寸的单位是什么?”。

为此,我使用 JCL“项目分析器 IDE”,它与 JCL/JVCL 安装集成在 IDE 中,它向您显示所有单元及其各自的大小。您可以将其导出为文本文件。
如果您在 2 个环境(D4 和 D2009)中执行此操作,您将获得很多相关信息。

Another way is to have a look to 'what unit increase the size ?'.

To do this, I use the JCL 'Project Analyser IDE', integrated in the IDE with the JCL/JVCL installation, it show you all the units with their respective size. You can export it in text file.
If you do it with the 2 environnements (D4 & D2009) you will have a lot of pertinent informations.

滴情不沾 2024-09-15 06:26:03

我做了一些测试来看看D2007和D2010之间的区别,因为我们正在升级到D2010。我测试了一个中型管理 GUI 应用程序,其中包含大约 60 个表单(带有详细表单、框架等的网格)。我们使用 TMS 组件 + Remobjects。

D2007:
“正常”编译:18.8mb
带调试 dcu:18.8mb(大小相同!)

D2010
正常:23.9
debug dcu's:48.8mb (!)

因此,使用 debug dcu's 会使我们的 exe 大小加倍...

使用我们的业务服务进行测试(没有大的 dfm):
D2007:12.3mb
D2010:17.1mb

所以,是的,D2010 增加了 exe(一点点),但这对我的客户来说不是问题。

编辑:有关编译大小的一些信息:
D2007:
替代文本
D2010:
alt text

因此增加了代码大小,但是数据增加一倍以上!

I've done some tests to see the difference between D2007 and D2010, because we are upgrading to D2010. I've tested a medium sized management GUI application, with about 60 forms (grids with detail forms, frames, etc). We're using TMS components + Remobjects.

D2007:
"normal" compilation: 18.8mb
with debug dcu's: 18.8mb (same size!)

D2010
normal: 23.9
debug dcu's: 48.8mb (!)

So using debug dcu's doubles our exe size...

Test with our business service (no big dfm's):
D2007: 12.3mb
D2010: 17.1mb

So yes, D2010 increases the exe (a bit), but this is not a problem for my customer.

Edit: some information about compiled size:
D2007:
alt text
D2010:
alt text

So an increase of code size, but a more than doubling of the data!

紫﹏色ふ单纯 2024-09-15 06:26:03

如果您不想使用 exe 压缩器,那么您应该尝试 StripReloc

If you don't want to use an exe compressor then you should give StripReloc a try.

有木有妳兜一样 2024-09-15 06:26:03

检查 dfm-s 的格式。如果你想让你的 exe 更小,它们必须是二进制格式。

Check format of your dfm-s. They must be in binary format if you want to make your exe smaller.

梦太阳 2024-09-15 06:26:03

1) 您正在生成一个详细的映射文件,并且由于您已设置“使用调试 dcus”,因此它还将包含 RTL/VCL 单元的符号。如果异常处理系统使用它来生成调用堆栈等,则可以将其添加到可执行文件中。如果不以某种方式压缩,它可能会使您的 .exe 大小变得相当大。

2) 使用 debug dcus 也会使您的 .exe 变得更大,因为通常它们是在没有优化和调试选项设置的情况下编译的,并且它们也会使您的代码变慢。它们不应该在发布版本中使用。

3) 调试信息应仅将 debig 信息添加到单元而不是可执行文件中,尽管需要 IIRC 来生成映射文件。

1) You are generating a detailed map file, and because you've set "used debug dcus" it will also contains symbols for the RTL/VCL units. If it is used by an exception handling systems to generate call stacks and the like, it could be added to the executable. And if not compressed somehow, it could make your .exe size pretty large.

2) Using debug dcus will also make your .exe somewhat larger because usually they are compiled without optimization and debug options set, and they will make also your code slower. They shouldn't be used in a release version.

3) Debug information should add debig info only to the unit and not to the executable, although it is required IIRC to generate the map file.

你的笑 2024-09-15 06:26:03

由于 D2010 添加了扩展 RTTI,而 RTTI 是增加 exe 大小的一个臭名昭著的因素,因此了解该应用程序的 D2009 二进制文件有多大将会很有趣。

如果 D2009 二进制文件明显较小,则它不是 Unicode 等。对于我自己的二进制文件,从 D7 到 D2009 仅增加了 30% 左右。

Since D2010 adds extended RTTI, and RTTI is a notorious factor in increasing exe size, it would be interesting to see how big D2009 binaries are for that application.

If D2009 binaries are significantly smaller, it is not Unicode etc. For my own binaries, I only have a 30% increase or so going from D7 to D2009.

我是有多爱你 2024-09-15 06:26:03

前面已经说过,使用可执行压缩器可以减小 exe 的大小,但不能减小安装包的大小。但是,如果您想要一个好的压缩器,请尝试 ASPack

@Tom1952:ASPack 相当快,只需几秒钟即可压缩一个文件

It has been stated earlier that using an executable compresser reduces the size of the exe but not of the install package. However, if you want a good compressor then try ASPack.

@Tom1952: ASPack is pretty fast, just a few seconds to compress a file

妄断弥空 2024-09-15 06:26:03

您也可以更改图标。最新的delphi IDE(即XE3)中的图标与Vista/7兼容并且包含所有尺寸(据我所知最大为256x256)。因此,您可以通过更改图标来减小 exe 文件的大小。

Also you can change the Icon. Icon in newest delphi IDE (ie XE3) is Vista/7 compatible and contains all sizes (up to 256x256 as far as I know). So you can reduce exe file size with changing the Icon.

久隐师 2024-09-15 06:26:03

较新的 delphi 中的标准单元可能包含更多字符串和常量,例如错误字符串,即使您禁用调试信息也会包含这些字符串和常量。检查你的用途。

除了不使用特定单元或从中删除不需要的数据之外,没有太多解决办法。

(我的经验是使用Delphi 5)

The standard units in you newer delphi may contain more strings and constants such as error strings, that is included even if you disable debug information. Check your uses.

Don't have much of a somution besides not using a specific unit, or removing unneeded data from it.

(My experiences are with Delphi 5)

高跟鞋的旋律 2024-09-15 06:26:03

对于默认设置的 Delphi 10.3 Rio:

步骤 1:在“Projects”窗口中从 Debug 切换到 Release。这将我的 exe 文件从 22 MB 减少到 5 MB!

第 2 步:使用 ASPack 等 exe 压缩器。它进一步将我的 exe 文件减少到 1.3 MB。难以置信,不是吗? :)

For Delphi 10.3 Rio with default setiings:

Step 1: Switch from Debug to Release in "Projects" window. This reduced my exe file from 22 MB to 5 MB !

Step 2: Use an exe compresor like ASPack. It further reduced my exe file to 1.3 MB. Unbelievable, isn't it ? :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文