14

This should reduce the executable size quite a bit in some of my very large projects. I am sure there would be other benefits too.

EDIT: Is there perhaps a utility that will scan the project and remove redundant ones automatically? I do have 100s of projects and "automatic remove" would be first prize although if I have to I will go the manual way with the help of identifying utilities.

Johan Bresler
  • 6,450
  • 11
  • 56
  • 77

7 Answers7

13

You might want to take a look at at CnPack.

CnPack includes a Uses cleaner wizard wich hasn't failed me yet.

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
  • Does it do an automatic remove if desired? – Johan Bresler May 19 '09 at 09:47
  • 4
    +1 for CnPack - it has some smart options which avoid common pitfalls like units with initialization sections – mjn May 19 '09 at 10:17
  • 3
    @mm2010, it has. As a matter of fact, it doesn't have a "show me what you are going to remove" option, only a report "after" the action of what was removed. Although I never had any problems with it, I recommend you have your source files backed up in a source control system. – Lieven Keersmaekers May 19 '09 at 11:24
  • It should be noted that CNPack uses cleaner only analyzes units used in the DPR, as far as I could tell it doesn't check the uses of the individual units. So it's not all that useful if your company policy is to go out of your way to delete that info. – Peter Turner May 19 '09 at 21:20
  • 2
    Newer versions of cnPack display a pretty treeview showing all intended operations, and iirc you can override (by unchecking) the actions. – mjn Oct 08 '10 at 17:37
  • CnPack may not be suitable as it doesn't work on large projects - anything over 200 units or so and it simply hangs. Depends on what the OP means by 'large'. (I suspect the optimisation task gets exponentially harder as you add more units, so it may be that no optimiser can do it.. icarus also fubar's quite badly on some of our larger projects). – Tony Hoyle Jan 21 '13 at 14:47
9

I used to use Icarus that gave me a report of unused uses entries.

From the web page

ICARUS parses Delphi or Borland Pascal source code and generates a Uses Report. This report will help you remove unneeded units from your uses lists. You will also know which units that can be moved from the interface uses list to the implementation uses list.

Removing unused uses references has multiple benefits: Cleaner code to maintain, no need to bother about code that is not used Code from initialization and finalization sections in unused units is not linked in Compilation runs smoother and quicker

ICARUS is very easy to use, just select a source file, set a few options, and start analyzing. Or let the built-in wizard guide you through this process.

ICARUS is FREEWARE.

Johnno Nolan
  • 29,228
  • 19
  • 111
  • 160
2

Such a tool can never analyze if the initialization or finalization sections of a used unit need to be executed or not. Removing units from the uses could result in not running initialization and finalization sections and therefore result in bugs.

Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • Maybe the remove can be an optional question in such a utility? – Johan Bresler May 19 '09 at 09:48
  • 1
    Units where only the initialization/finalization sections are important are quite rare. In general, these sections are used to initialize code for the unit itself, not for the generic application. Furthermore, a tool like ICARUS does check for these sections or can be made to analyze those sections and check if they're important. – Wim ten Brink Oct 08 '10 at 13:48
2

If your code can be compiled with Free Pascal, the Free Pascal compiler also will emit hints about unused units in your code.

See also this SO entry with additional information about possible problems, like RTTI based methods:

https://stackoverflow.com/questions/605977/tools-to-detect-dead-code-in-delphi2007-or-above/

Community
  • 1
  • 1
mjn
  • 36,362
  • 28
  • 176
  • 378
2

Remember that the linker will strip anything that can't be called. If no use is made of a unit and it has no initialization or finalization it's going to be completely stripped anyway. Anything referenced in the initialization or finalization will stay but in general this shouldn't bring in much anyway.

It's possible for a unit to consist of only initialization so full-auto stripping is limited anyway. Units which merely change system behavior in some fashion might very well have no references and yet be needed. (The replacement memory manager with good leak tracking comes to mind. While it does have things you can reference there's no need to, just use it and get the leak report. Nice double-check for your code.)

Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45
1

See some of the answers here.

Community
  • 1
  • 1
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
1

Peganza has similar tools: http://www.peganza.com/

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • 2
    The reports generated by Icarus are overcrowded with unnecessary info. Really difficult to figure out what they try to show you. – Gabriel May 10 '14 at 08:54