0

How do you save space by preventing the inclusion of unnecessary binaries?

When I compiled the ROS Blink example, I received the warning:

Global variables use 1,951 bytes (76%) of dynamic memory,
leaving 609 bytes for local variables. Maxiumum is 2,560 bytes.
Low memory available, stability problems may occur.

Considering the example uses almost no libraries, I was surprised by this, but sure enough, the compiler pulls in a ton of libraries that are never used:

Scanning dependencies of src
Scanning dependencies of arduino
Scanning dependencies of ros_lib
src/main.cpp
arduino/wiring.c
arduino/wiring_shift.c
arduino/wiring_digital.c
arduino/wiring_pulse.c
arduino/WInterrupts.c
arduino/hooks.c
arduino/wiring_analog.c
arduino/USBCore.cpp
arduino/new.cpp
arduino/Tone.cpp
arduino/WString.cpp
arduino/HardwareSerial1.cpp
arduino/Stream.cpp
arduino/HardwareSerial3.cpp
arduino/HardwareSerial0.cpp
arduino/CDC.cpp
arduino/HardwareSerial2.cpp
arduino/main.cpp
arduino/HID.cpp
arduino/HardwareSerial.cpp
arduino/IPAddress.cpp
arduino/Print.cpp
arduino/WMath.cpp
arduino/abi.cpp
Linking libarduino.a
ros_lib/duration.cpp
ros_lib/time.cpp
Linking libros_lib.a
Linking firmware.elf
Converting to firmware.hex

Why are so many files being included in the compilation, and how do I filter them out?

  • 2
    It's 2015. What is compiled does not always become what is linked. – Ignacio Vazquez-Abrams Dec 14 '15 at 4:12
2

The Arduino IDE build script will actually copy all core source code file to the tmp directory, compile and generate an archive file. The sketch is compiled and linked with this library. Only the referenced (used) functions and data are linked in the binary.

The size (both program and data) of this blink is somewhat larger than the "standard" blink example but this is due to the publisher-subscriber run-time.

Cheers!

PS: If you google you can find commands to listing what is actually in the generated binary. Here is a starting point.

| improve this answer | |
2

Part of the compilation process is what is known as garbage collection, which is controlled by the compilation flag -Wl,--gc-sections

This process goes through the compiled program and works out which sections (functions, blocks of read-only data, etc, each in its own section controlled by -ffunction-sections and -fdata-sections IIRC) and works out which are not needed in your compiled program. It then deletes them.

The problem you have though is not with code or read-only data size, but with static RAM allocation size. This is global variables and local static variables - things used for stiring data, maybe as serial buffers, etc. Most of that is probably part of the underlying RTOS that you have elected to use. Which is the problem of using an RTOS to just blink an LED. It could be seen as a task that is beneath the RTOS and is complete overkill. Of course, since you're just testing, that is pretty irrelevant, however, as you add more functionality you will most likely find that the memory footprint doesn't increase much since it's the "background" stuff that of the RTOS that will always be there regardless of what tasks you are performing.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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