Libraries and memory

Hey everyone!

I am experimenting with the adafruit 10 DOF sensor (Introduction | Adafruit 10-DOF IMU Breakout | Adafruit Learning System) and the adafruit microSD breakout board(Introduction | Micro SD Card Breakout Board Tutorial | Adafruit Learning System).

I have combined the pitchrollheading example provided by Adafruit with the SD datalogger which comes as an example with the IDE.

Everything is running just fine. However, my sketch needs 7(!) libraries:

#include #include #include #include #include #include #include #include

which amounts to a whopping 71% of memory of my Uno.

Now to my question: Is there a way to compress these libraries? Should I copy the code I need in my sketch? should I make my own library?

I have tried to read into them but I don’t quite understand them. From what I understood they didn’t seem to be bloated…

I am very grateful for any help (=

The compiler will already only build functions that you call (I have been in situations where I had to rewrite my sketch to avoid using pinMode() to fit the code...). You save nothing by putting the same code into your own files.

How much of that is the SD card, and how much is the 10DOF stuff? Actually, I was curious and am at a computer with the IDE - on 1.6.10 with an empty sketch I get 36% with just the SD library. SD card library is kinda big - talking to SD cards is kinda complicated. It looks like the 10DOF stuff is about as much again. My suspicion is that Adafruit's library could be slimmed down - their libraries are always optimized for ease of use (by the arduino programmer) rather than compile size.

You might consider a board with more flash ( mega 2560, or one of the atmega1284-based boards - if you want to stick to 5v AVR-land)

Also - it's best to be clear when talking about memory which memory you're talking about. You're talking about usage of flash or program memory. There's also ram, or dynamic memory, and usage of that can also be a problem.

SD cards as I understand it have a file system - allocation tables, directories, the works. This means that the SD library is effectively a mini Disk Operating System. Its complicated, it's a lot of code.

DrAzzy: The compiler will already only build functions that you call (I have been in situations where I had to rewrite my sketch to avoid using pinMode() to fit the code...). You save nothing by putting the same code into your own files.

Actually that is incorrect on both counts. The compiler always generates object code for all functions in a source module. It is the linker that removes unreferenced symbols, which includes functions that are not called. It requires that the compiler be told to put each function into its own memory section and then the linker has to be told to do this "dead code" optimization. The Arduino IDE by default turns on this optimization.

On the second count it can make a difference where/how the code is compiled. That is because the compiler can sometimes do more optimization when the code is compiled together vs compiled seperately and then linked together.

So while typically there is no need to make one gigantic blob of code, it can potentially (and usually will) generate a bit smaller code. While it may offer a little bit of reduction, it often isn't much.

However, the new AVR gcc compiler that comes with IDE 1.6.10 has some extensive code folding capabilities that can do better link time optimization that offers some additional code optimizations.

So one easy alternative to get things a bit smaller would be use IDE 1.6.10 if you are not already using it.

--- bill

Thank you for your great answers!

DrAzzy: How much of that is the SD card, and how much is the 10DOF stuff?

The datalogger and the pitchrollheading sketches on their own both take 43%.

I have compiled an empty sketch with just the #include statements and it only was 4% so @DrAzzy and @bperrybap you were right.

DrAzzy: it's best to be clear when talking about memory which memory you're talking about.

You are right. I forgot that sorry.

bperrybap: the new AVR gcc compiler that comes with IDE 1.6.10 has some extensive code folding capabilities that can do better link time optimization that offers some additional code optimizations.

Sounds promising! I am in 1.6.9 so I will definately give it a try.

DrAzzy: You might consider a board with more flash.

that would probably be the best solution but I wanted. However, I wanted to maintain a small footprint. I originally wanted to use the micro - I guess I better get that idea out of my head haha.

Again thank you for your help!

MarkGoingToSpace: However, I wanted to maintain a small footprint. I originally wanted to use the micro - I guess I better get that idea out of my head haha.

If you want small with lots of flash and RAM and need an AVR consider using a teensy++ board. 128k flash and 8k of ram.

If you don't need AVR then consider the Teensy 3.x boards as you can have even more flash, more ram and MUCH faster including 32bit support and no @#$@# progmem crap to deal about. const "just works" like it should. https://www.pjrc.com/teensy/index.html

Some of the Teensy boards are 3v only so if you need 5v compatibility make sure to select one that supports that.

Also, Paul does a great job fixing many things in the IDE and core code and things like digitalWrite() are up to 100 times faster on AVR based Teensy boards than on other AVR platforms.

--- bill

bperrybap: If you want small with lots of flash and RAM and need an AVR consider using a teensy++ board. 128k flash and 8k of ram.

:o I didn't know that something like this exists! Looks awesome. Thank you!