How to enable maximum Dead Code Removal? I am running out of code and data memory space on ATMega328P. 

Code size is big as I used several libraries, but, I only use a few functions of those library. 

Apparently, the default IDE is only doing a partial job in dead code removal (remove unreferenced code and data). 

Some experiments shows:


A normal program that uses several Arduino libraries is 22 kilo bytes in code size.
Rename setup to o_setup. 
Rename loop to o_loop

Add 

void setup(){};

void loop(){};

Code size is 8 kilo bytes while the program is 'empty' effectively.
Start a new program. 

Add 

void setup(){};

void loop(){};

Code size is 0.5 kilo bytes  


Apparently, the IDE (linker called by IDE) is doing a 'partial' dead code removal job, as reducing code from 22kB to 8kB (case 1 and 2), instead of 0.5kB (case 3). 

How can I enable maximum dead code removal function (reclaim code space occupied by unused library functions)?
Enable your IDE's verbose of the compile via the preferences. From there you can see exactly what and how it is being compiled.

In short the IDE (and toolchain) will compile everything and anything it can find into an object. Where only the used code is compiled in the linking phase, as garbage collection is enabled with the "--gc-sections" command.

Note that the optimization level is "-Os" or "Optimize for size". In IDE 1.0.+ this appears to be hardcoded. Where as in IDE 1.5.+ the compiler options are configurable, in "\arduino-1.5.6-r2\hardware\arduino\avr\platform.txt" can be customized to meet your needs.

Where you can use the avr-objdump command as described in https://stackoverflow.com/questions/15291750/does-importing-libraries-load-everything-in-arduino/15296808#15296808. 

I would recommend 1.5.6r2 and not 1.5.7 as the gcc commands won't run individually without some path manipulation.