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)?
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.
-
1you 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– mpflaga2014年09月27日 03:37:43 +00:00Commented 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.fuenfundachtzig– fuenfundachtzig2015年06月05日 17:57:06 +00:00Commented Jun 5, 2015 at 17:57
#include
a library but not use it can still add a lot of code in the link process.#include <Servo.h>
Servo myservo;
. HereServo myservo;
actually creates in instance of the Servo class, with all it's memory it needs for its private variables.