Very large program storage generate in Arduino

hamsafar_a85
Tue May 09, 2017 11:41 am
I compile my project for UNO and my program size is about 16kB but when compile it for Maple mini code is too big!!!
Sketch uses 85056 bytes (76%) of program storage space. Maximum is 110592 bytes.
Global variables use 6272 bytes of dynamic memory.

How can I reduce the program space?


stevestrong
Tue May 09, 2017 11:53 am
Avoid using
blabla my_variable = new some_fancy_class;

victor_pv
Tue May 09, 2017 12:33 pm
Another member of the forum created this tool to analyze the .map file:
http://danieleff.com/stm32/map_analizer/

If you don’t use “new” or think that doesn’t account for all the space difference, get your file from your build folder, upload it, and will help you find what’s taking space. If you have doubts with anything you find there post back to see if someone can give you extra advise.


ag123
Tue May 09, 2017 7:09 pm
check inside your platforms.txt on the particular core e.g. F1 or F4 these flags should be specified on the gcc and g++ compile flags
-fmessage-length=0
-fsigned-char
-ffunction-sections
-fdata-sections
-ffreestanding
-fno-move-loop-invariants
-fno-exceptions
-fno-rtti
-fno-use-cxa-atexit
-fno-threadsafe-statics
-nostdlib
-Xlinker --gc-sections
--specs=nano.specs

hamsafar_a85
Wed May 10, 2017 4:34 am
stevestrong wrote:Avoid using
blabla my_variable = new some_fancy_class;

hamsafar_a85
Wed May 10, 2017 4:40 am
victor_pv wrote:Another member of the forum created this tool to analyze the .map file:
http://danieleff.com/stm32/map_analizer/

If you don’t use “new” or think that doesn’t account for all the space difference, get your file from your build folder, upload it, and will help you find what’s taking space. If you have doubts with anything you find there post back to see if someone can give you extra advise.


ag123
Wed May 10, 2017 6:39 am
check in platforms.txt that these compile flags are specified in gcc, g++ compile commands
-nostdlib
-Xlinker --gc-sections
-specs=nano.specs
-fno-exceptions
-fno-rtti
-fno-use-cxa-atexit
-fno-threadsafe-statics

RogerClark
Wed May 10, 2017 10:14 am
The problem with new causing the binary size to be much larger than expected was partially fixed last year in this commit

https://github.com/rogerclarkmelbourne/ … 2a824d80b7

By the addition of new.cpp

However, it does not seem to work in all circumstances

Someone will need to investigate why the functions in new.cpp are not being called in this case


hamsafar_a85
Sat May 13, 2017 7:30 am
ag123 wrote:check in platforms.txt that these compile flags are specified in gcc, g++ compile commands
-nostdlib
-Xlinker --gc-sections
-specs=nano.specs
-fno-exceptions
-fno-rtti
-fno-use-cxa-atexit
-fno-threadsafe-statics

stevestrong
Sat May 13, 2017 7:32 am
Can you show us your code?

hamsafar_a85
Sat May 13, 2017 7:40 am
RogerClark wrote:The problem with new causing the binary size to be much larger than expected was partially fixed last year in this commit

https://github.com/rogerclarkmelbourne/ … 2a824d80b7

By the addition of new.cpp

However, it does not seem to work in all circumstances

Someone will need to investigate why the functions in new.cpp are not being called in this case


racemaniac
Sat May 13, 2017 7:51 am
that’s indeed a pretty normal baseline :)
all the basic stuff takes up more space than what you see on an arduino, but you have plenty of space to continue working with :)

hamsafar_a85
Sat May 13, 2017 7:59 am
stevestrong wrote:Can you show us your code?

hamsafar_a85
Sat May 13, 2017 8:11 am
racemaniac wrote:that’s indeed a pretty normal baseline :)
all the basic stuff takes up more space than what you see on an arduino, but you have plenty of space to continue working with :)

stevestrong
Sat May 13, 2017 10:04 am
The FLASH and RAM values you get by comparing to UNO are normal.
Don´t worry, any further added code does not increase the code size proportionally.

Can you also share a map file? Looking to the MAP file you can determine which modules causes the largest code size.
It seems that the code you shared is not all what you are using, there are some libraries missing.


edogaldo
Sat May 13, 2017 10:45 am
hamsafar_a85 wrote:with STM32F103C8 IC is:
Sketch uses 12876 bytes (19%) of program storage space. Maximum is 65536 bytes.
Global variables use 2816 bytes of dynamic memory.

While this code with UNO is:
Sketch uses 928 bytes (2%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.


stevestrong
Sat May 13, 2017 12:11 pm
hamsafar_a85 wrote:I compile my project for UNO and my program size is about 16kB but when compile it for Maple mini code is too big!!!
Sketch uses 85056 bytes (76%) of program storage space. Maximum is 110592 bytes.
Global variables use 6272 bytes of dynamic memory.

How can I reduce the program space?


Rick Kimball
Sat May 13, 2017 12:24 pm
A better benchmark would be to use an Arduino board that is comparable to the stm32f103. Our code size looks pretty good when you compare it to the Arduino Due. That core uses uses ~22k for a blink sketch. The Due also uses a cortex-m3 so at least you are comparing apples to apples.

Sketch uses 22124 bytes (4%) of program storage space. Maximum is 524288 bytes.

For grins I tested against my msp430g2553 arduino port:

Sketch uses 808 bytes (4%) of program storage space. Maximum is 16384 bytes.
Global variables use 18 bytes (3%) of dynamic memory, leaving 494 bytes for local variables. Maximum is 512 bytes.

The bottom line, the size of the code depends on the chip architecture, the compiler used, the actual core code, and how you code your program.


Rick Kimball
Sat May 13, 2017 12:45 pm
hamsafar_a85 wrote:stevestrong wrote:Can you show us your code?

ag123
Sat May 13, 2017 1:42 pm
among the differences, uno probably won’t support many things that’s *just there* on stm32, that we mostly ‘take for granted’
e.g. on stm32 there is usb-serial and stm32f1 is a native usb device, uno *don’t do usb and can’t do usb*, stm32 also has many more other resources such as 2 SPI ports, uarts, ADCs, DMA, clocks that can be programmed on the fly, turning on / off clocks to individual peripheral buses, multiple AFIO assignments, low power states etc etc. having support for these hardware may inevitably require more codes to support it hence a somewhat bulkier binary size

hence my guess is when compilers are building for atmega328, things that is ‘not there’ is simply *skipped* as the mcu won’t have the hardware to support them and would result in compile errors if they are built after all

the FastLeds library seemed rather large i’d think, i’m not too sure if it contain ifdefs that perhaps to skip over sections of code that’s not used, otherwise when the library is compiled, all the related objects may be included in the binary hence contributing to binary bloat as well


danieleff
Sat May 13, 2017 2:59 pm
hamsafar_a85 wrote:
with STM32F103C8 IC is:
Sketch uses 12876 bytes (19%) of program storage space. Maximum is 65536 bytes.
Global variables use 2816 bytes of dynamic memory.

hamsafar_a85
Sun May 14, 2017 3:56 am
stevestrong wrote:hamsafar_a85 wrote:I compile my project for UNO and my program size is about 16kB but when compile it for Maple mini code is too big!!!
Sketch uses 85056 bytes (76%) of program storage space. Maximum is 110592 bytes.
Global variables use 6272 bytes of dynamic memory.

How can I reduce the program space?


hamsafar_a85
Sun May 14, 2017 3:58 am
Rick Kimball wrote:hamsafar_a85 wrote:stevestrong wrote:Can you show us your code?

hamsafar_a85
Sun May 14, 2017 4:10 am
ag123 wrote:among the differences, uno probably won’t support many things that’s *just there* on stm32, that we mostly ‘take for granted’
e.g. on stm32 there is usb-serial and stm32f1 is a native usb device, uno *don’t do usb and can’t do usb*, stm32 also has many more other resources such as 2 SPI ports, uarts, ADCs, DMA, clocks that can be programmed on the fly, turning on / off clocks to individual peripheral buses, multiple AFIO assignments, low power states etc etc. having support for these hardware may inevitably require more codes to support it hence a somewhat bulkier binary size

hamsafar_a85
Tue May 16, 2017 7:12 am
Wow
After a while, and try and error I add -fno-threadsafe-statics

Leave a Reply

Your email address will not be published. Required fields are marked *