How to minimize library size

The ArduinoBLE library dramatically increases program size and for OTA purposes I am trying to keep my program as compact as possible.

Compiling an empty sketch with #include <ArduinoBLE.h> results in using 19% of program storage space. Looking at library files one can see it is fairly large, at least compared with other common libraries, and that it offers tons of functionality and methods.

I wonder if there's a way to minimize the space the library uses. maybe by reducing available functionality and keeping only what's needed, but without modifying the library itself which for me is the least prefered path, I want it to keep getting updates and stay standard.

Thanks!

You can certainly make your own reduced-option version of any library, but updates are of course lost.

Library support is entirely volunteer labor, so ask whoever maintains the one of interest if there is a way around that.

Your opinion of "only what is needed" will probably differ from someone else's.

I assume this wasn't clear so let me elaborate. I am not asking for a general solution, a smaller library by whoever wrote or maintain it, I am asking how I can reduce the space by for my needs, a subset of functionality based on what is needed for my program.

Download the library, cut out the stuff you don't need, test the rest and if it all works, you are done.

The library can go in the folder with your code (no need to #include .cpp files, they are automatically included). That way it can't easily be confused with other versions.

Due to its advanced structure, this particular library requires recursive compilation, meaning it must be placed in the src subfolder of the sketch, rather than being placed in the sketch root folder as is possible for libraries with a flat layout.

As mentioned in the OP:

I was more thinking of a solution involving Defined Constants or some other mechanism that tells the compiler to use only what is needed for a particular program but maybe nothing like this exists and the simple answer is that it is not possible.

This is exactly what the compiler does already.

To some extent, once an empty sketch with lib included takes 19% of program space I can only assume it is all or nothing. Is that wrong?

Well 19% is the minimum required. Then when you start using the library you might get some more.

How large is a similar sketch without the library?

Empty sketch 12Kb (4%)
Empty sketch with #include <ArduinoBLE.h> 50Kb (19%)
LedControl example 52Kb (19%)
PeripheralExplorer example 54Kb (20%)

Thx - still plenty to play with :wink:

Not at all. With WIFI, ETH, MQTT., OTA, JSON, WDT... and my code I am at 47% which is beyond the point OTA can work properly.

I already know I should find an alternative for Arduino_JSON which also take a lot of space but BLE is a different story and has no alternative.

This might help for writing json

Or reading in place

Interesting, thanks!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.