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:

  1. A normal program that uses several Arduino libraries is 22 kilo bytes in code size.

  2. Rename setup to o_setup. Rename loop to o_loop


    void setup(){};

    void loop(){};

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

  3. Start a new program.


    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)?

  • 1
    Be aware that some libraries declare global variables, instances of a class, for which the constructor code (and all its dependencies) will be linked into the final program. Hence the simple fact to #include a library but not use it can still add a lot of code in the link process.
    – jfpoilpret
    Commented Sep 25, 2014 at 18:29
  • I need all the lib as they are physical hardware chip drivers. But, I only use a few functions of each lib. Could you kindly explain "instances of a class, for which the constructor code"? Global var can be 'optimized/removed' by linker which see that it is declared by never used, by looking at assembly language linkage map, right?
    – EEd
    Commented Sep 25, 2014 at 18:44
  • 1
    In case 2 did you also remove all the code outside of loop2 and setup2. E.g. to use the Servo library you'd use #include <Servo.h> Servo myservo;. Here Servo myservo; actually creates in instance of the Servo class, with all it's memory it needs for its private variables.
    – Gerben
    Commented Sep 25, 2014 at 19:52
  • Can you list the libraries you require?
    – Craig
    Commented Sep 25, 2014 at 20:40
  • I have several hardware sensor boards and each has its own needed lib (driver) to work. For example, the LCD driver has many functions for drawing line, circle, pie, fill, etc. but I only use the draw 3D text mode. In worst case, I can manually delete un-used part of the library source. Can take time as inter-dependency is everywhere. The LINKER should be able to do this task automatically. The question is how to turn it on and command it to perform? Compiler can still allocate code/ram space. linker is final gate keep who has full and latest info, which code where, call who and called by who
    – EEd
    Commented Sep 25, 2014 at 20:45

1 Answer 1


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.

  • 1
    you can try to change the -Os to -O2 or -O3 to get smaller. S is supposed to include 2. However, I have seen it in some cases get smaller.
    – mpflaga
    Commented Sep 27, 2014 at 3:37
  • 1
    -O2 and -O3 ask the compiler to optimize its output for speed, not for size. Thus the compiled binary will get larger rather than smaller. Commented Jun 5, 2015 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.