36

I have a class library and am using only part of it. Is there a need to delete what isn't being used in order to shrink the size of the created code (in release configuration)?

As far as I've seen, the compiler takes care of that, and removing the code doesn't change the EXE file size. Will this always be true? Removing all unneeded code would take very long, so I want to know if there's need for that.

More information: there are methods and classes in the class library that aren't called from the executing code, but are referenced by other parts of code in the class library (which themselves are never called).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • 3
    You could always grab .NET reflector and see just what is included in the "released" DLL. Best way to learn is to experiment (or so has been my experience) – Brad Christie Apr 17 '12 at 13:22
  • 3
    Is the compiler going to be able to tell that you will never distribute that DLL to somebody who may try to use that code? – Anthony Pegram Apr 17 '12 at 13:22
  • 1
    @AnthonyPegram The library is not compiled separately. I have the source code in my project. – ispiro Apr 17 '12 at 13:24
  • 2
    "removing the code doesn't change the .exe file size" note that .NET .exe files are usually rounded up to an exact number of pages, or multiple of pages - either 4K or 16K, I forget. – Rup Apr 17 '12 at 13:26
  • 2
    @BradChristie Unfortunately that's for a minimum of [$70](http://www.reflector.net/). – ispiro Apr 17 '12 at 13:33
  • @Rup Thanks. That explains why it didn't make a difference when I removed a bit of code. – ispiro Apr 17 '12 at 13:36
  • @ispiro: You can get a 30-day trial, ya know. ;-) – Brad Christie Apr 17 '12 at 13:44
  • 3
    ILSpy is a free .NET decompiler/reflector: http://wiki.sharpdevelop.net/ILSpy.ashx That's what I used to verify my thought that the compiler includes dead code. – Tim S. Apr 17 '12 at 14:30
  • @TimS. Thanks. That can come in handy. – ispiro Apr 17 '12 at 15:57

6 Answers6

41

No, the compiler includes the "dead" code as well. A simple reason for this is that it's not always possible to know exactly what code will and won't be executed. For example, even a private method that is never referenced could be called via reflection, and public methods could be referenced by external assemblies.

You can use a tool to help you find and remove unused methods (including ones only called by other unused methods). Try What tools and techniques do you use to find dead code? and Find unused code to get you started.

Community
  • 1
  • 1
Tim S.
  • 55,448
  • 7
  • 96
  • 122
6

It all gets compiled. Regardless of whether it is called or not. The code may be called by an external library.

The only way to make the compiler ignore code is by using Compiler Preprocessor Directives. More about those here.

nickm
  • 1,775
  • 1
  • 12
  • 14
0

I doubt the compiler will remove anything. The fact is, the compiler can't tell what is used and what is not, as types can be instantiated and methods called by name, thanks to reflection.

Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53
0

Let's suppose there is a class library called Utility. You created a new project and added this class library to that project. Even if your EXE calls only 1-2 methods from the class library, it's never a good idea to delete the unreferenced code.

It would go against the principle of reusablity. Despite the fact that there would be some classes present in the library unreferenced from the EXE, it would not have any bad impact on performance or size of the program.

Ry-
  • 218,210
  • 55
  • 464
  • 476
FIre Panda
  • 6,537
  • 2
  • 25
  • 38
0

Determining all and only dead code is (if one makes the idealization that one has a "math world" like language) recursively undecidable, in most languages. (A few rare ones like the Blaise language are decidable.)

Keith Douglas
  • 102
  • 1
  • 8
0

to the question of whether there is a "need to delete what isn't being used in order to shrink the size of the created code": i think this would only be useful to save network bandwidth. removing unused code is crucial in web applications to improve loading speeds etc.

if you're code is an exe or a library, the only reason i see to remove dead code, is to improve your code quality. so that someone looking at your code 2 years down the line won't scratch their heads wondering what it does.

Nandun
  • 1,802
  • 2
  • 20
  • 35