-2

Possible Duplicate:
Reduce exe file

What are some tools that, given an EXE file, remove all unused code and make a new EXE file with code really used by the application? I think that something like that should exist. Just for curiosity; but I think that it can be really good as tools for producing a smaller EXE file without unused code.

P.S.: Delphi produces standalone EXE files that contain code of all object used in an application, but not all elements of this object are really used. This makes big files. I remember the first version of Pascal that include only code really used and did not insert unused code, and EXE files were smaller.

Community
  • 1
  • 1
Marcello Impastato
  • 2,263
  • 5
  • 30
  • 52
  • 1
    Possible duplicate, [Reduce exe file](http://stackoverflow.com/q/7398580/576719). – LU RD Nov 05 '12 at 21:36
  • 1
    You've asked this question already – Roddy Nov 05 '12 at 21:57
  • Yes is true, but that time i asked about as to do using delphi. And there was gived suggest about it. Now i, yes i asking similar thing, but looking the problem from a other side. I understand you of course, but now i asking about some tools that having an exe file "scan" it and clean all unused code rewriting exe file with used code only. In this is the difference with that question of some years ago. – Marcello Impastato Nov 05 '12 at 22:01
  • It's apparent that Marcello is still working on the same problem, @Lurd, but this is not the same question. This question asks about ways to take *an EXE file* and remove unused code. The previous question accepted an answer that involved changing compiler and linker settings; that technique obviously won't work on the already-compiled output EXE. – Rob Kennedy Nov 05 '12 at 22:16
  • 3
    @Marchello : No tool exists that will modify EXEs safely reducing the code inside. To even ask the question is to show a lack of understanding of how difficult this task is even at the Linker stage. – Warren P Nov 05 '12 at 23:01
  • And there will be no such tool, because such tool should test the exe with all states that could ever reached, to get the parts that are really not used. For some exes you will need some years to get the desired result. And the effort? Saving some MB (just worth some cents) but spending lots of money just for energy. – Sir Rufo Nov 05 '12 at 23:48
  • @Marcello, the options to reduce a standard exe file were given by RBA in your other question (like an exe-packer, upx). – LU RD Nov 06 '12 at 07:22

2 Answers2

1

Current Delphi also excludes unused code. You can easily see this in Delphi. If you compile, you get 'blue dots' in the gutter of your code. Functions that are not used don't have blue dots, meaning they are excluded. If you check the Optimization checkbox in project options, code is rewritten to make more use of registers thus eliminating certain variables.

Nevertheless, Delphi executables grow, especiall when using certain units. I think the smallest .exe you can create in Delphi 7 is about 10Kb. In later versions this will probably be a bit larger.

Important causes of file size is

  1. resources. Large or lots of images consume a lot of space. If you use icons on multiple forms, make sure to put them in a central image list (on a shared datasource). Use one of the available PngImageList implementations for smaller image size against better quality.
  2. rtti. The runtime type information causes class definitions to consume extra space. Thise space is partly due to the meta information about the class, but mainly because all extra code. Any methods that may be called using RTTI could be bound in a kind of 'late binding' fashion. Therefor, the compiler cannot know whether the methods can be eliminated, so it needs to include them in the executable.
  3. registered classes. Similar to 2. If a class is registered, it can be fetched and instantiated using its name as a string. Those classes must be included in the project, even if they are never used, just because the compiler cannot know if they are needed.

It's a fact that RTTI is expanded in recent Delphi versions. I think this also causes the RTTI meta information about classes to consume more space. There's only so much you can do about that.

In general, I think the Delphi compiler still does a lot of optimizing. You shouldn't have to worry about exe file size. If you do, you could try a packer like upx. I've got good esperiences with upx. It cuts down the executables to about a fifth or less of their original size, while retaining all functionality.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • Hello Golez, i have downloaded upx and like me. I have read about all that you told explaining cause that make big exe file. He we talk of delphi of course, but in general i tell about any exe file, not necessary generated of delphi. I ask simply if there is some tools that remove died-code (or unused code) in an exe file. PS: Thanks to Rob for to have understood better mine question. – Marcello Impastato Nov 05 '12 at 22:45
  • Due to the two Delphi-tags and the mentioning of Delphi vs Turbo Pascal in your question, I really didn't see that this is about executables in general. However I noticed you talked about executables, which is why I mentioned UPX. UPX works on all executables, not just those built with Delphi, but if you got the source, I would work with that. – GolezTrol Nov 06 '12 at 06:44
0

If you application is huge, check if you don't have a lot of debug info compiled in. Try a release build and check if it's any smaller.

What i sometimes do is configure the project to compile all .dcu files into a single folder. That way you quickly see all of the useless units that get compiled into your .exe. It often happens that you include a single unit for a single function, but in turn you get a whole tree of dependent units. You'll just have to search your uses clauses and try to get rid of these dependencies somehow.

I think both gexperts and cnpack contain tools to show dependencies, or to scan for unused units in your project. They can be helpful with this.

After you've removed useless dependencies, you can always compress your compiled exe with upx. There are supposed disadvantages to that (barry kelly wrote about it some time ago), but i've got good experiences with it. It sometimes makes the file 4x as small which can be a big deal.

Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
  • Hello Wouter, thanks for answer. Yes i know about you told. No in general i wanted to try to look the question from other side: having an exe file, there is some utilities that "scan" this exe file and clean all code unused rewriting a new exe file that has only code really used? The result should to be a new file of smaller dimension. – Marcello Impastato Nov 05 '12 at 21:45