How to reduce program size

  • Hi
    I want to include signing into my project but it makes my program too big to fit into atmega328p ... What I found is that the "super duper" arduino ide is just compiling all the libraries from the libraries directory as they would be a part of the project, not as a real libraries. So the linker pulls all the functions from those "libraries" into the resulting binary together with not used functions. So they are sitting there doing nothing taking the space out of my binary 😞 Is there an easy way to fix that problem ? I can only think of writing a proper makefile to build my project but this will take some time to get it right ...

  • hmm, I think that the arduino strips unused functions by using -fprogram-sections and -Wl,--gc-sections, and those functions I thought that are not use are actually used.

  • Contest Winner

    @rozpruwacz What happens if you just remove the unused libraries from the folder? If the compiler fails, they are used in one way or another.

  • Mod

    Unused functions are not included in the binary. might help a bit to reduce the footprint.

  • @rozpruwacz I can guarantee you that the IDE will NOT link all the libraries in every project. I have many, and they would never fit in one 328P memory. Even the parts of libraries that are not used do not get in.

    That said, there are many "memory eaters" in libraries, so you should only consider using one if you really need it.

    A second point of attention is using floats. They are deadly for memory, because the 328p is not able to calculate with floats natively. Division is already huge, but if you use any sin or tan, kB are used.

    A third eater is ... objects. When you create an object (from a library), every part of it is created. So if it done according to the good old rules, it starts bay uselessly copying the parameters of the constructor into its internal variables ... that maybe won't ever change.

    Strings are also eating a lot of memory. Both in creation and in manipulation.

    To see what eats your memory, start from an empty sketch setup+loop, and add each part of your sketch (in a compilable way) step by step. You could be surprised of where the biggest memory eaters are.

  • Mod

    avr-size and readelf might help you track what's using up the space. Some details in (focused on ram usage, but the same tools can show information about the flash usage)

  • wow, didn't expect such a response 🙂 thanks.
    every think is working as expected. I was mislead by some functions from the APDS9930 library that I was not using, but the library was using them in its init function. I solved my problem by reducing size of my code and additionally I removed some functions from the APDS9930 library.

    So, bottom line. The Arduino IDE uses -fprogram-sections and -Wl,--gc-sections switches during compilation. Those switches remove the unused parts of the compiled objects from the resulting binary.