Is it possible to reduce a sketch size be modifying libraries?


Does anyone have experience modifying libraries (specifically HttpClient) in order to reduce the size of a sketch?

I’m writing a sketch and I want my arduino uno (+plus ethernet/SD card shield) to be able to:
-log data to Cosm
-log data to SD card
-read information from a DHT11 temperature/humidity sensor
-get the current UTC time from an internet server

I’ve found example codes that do all these things individually, and I verified that in individual sketches each component works. However, when I combine everything together in my master code I get an error saying that my sketch is too big (37.4 kb out of 32.3 kb allowed). I have a few ideas on how to reduce the size of my sketch, but with all the libraries I need to include for the above tasks,
#include <dht11.h>
#include <SD.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <HttpClient.h>
#include <Cosm.h>,
I think it might be better for me to tackle it from that end. The biggest seems to be the HttpClient that is required for the Cosm feature to work properly, with HttpClient.h at 20kb and HttpClient.ccp at 17kb (according to windows explorer).

Could I remove parts of those libraries that Cosm doesn’t need to make it smaller? Has anyone tried this before or know what parts I could remove? Or is there another way to upload data to Cosm that doesn’t require the HttpClient.h library? Or is it possible to store part of the program or library files on the SD card on the board?

SD.ccp is also large, at 15kb. Do both the .h and .ccp file become part of the final sketch size? I would assume not, since otherwise that would be much more than the 37kb my sketch is currently at, unless it gets compressed during compiling…, so which is more important to focus on?

thank you for any advice or tips!


The size of a source file has little to do with the size of an object module. The 20kb and 17kb don't add up to create a 37kb object module.

Could I remove parts of those libraries that Cosm doesn't need to make it smaller?

That's the linker's job, and it is really good at it. You don't call a function, the linker doesn't include it. You do, and it does. The only way to stop the linker from including something is to stop calling it.

Or is it possible to store part of the program or library files on the SD card on the board?


Do both the .h and .ccp file become part of the final sketch size?

See above.

so which is more important to focus on?

The code you control.

Don't bother.

As I understand it, what you are trying to do is done for you when the programme is compiled.

It is a tempting avenue, particularly as you are probably around 35k at the moment and looking to snatch victory from the jaws of defeat with a little judicious shaving. Besides, your SD card suggests you may want to add an on-board clock later, which will only make the situation worse.

$25 will see you with a Mega and all your problems are solved.

You might also want to check your data types. If you are using integers where bytes will suffice, you can save a lot of room depending on how many you are using.

Strings are also very expensive. If you are using a lot of fixed strings, it is better to store them on your SD card or stick them in the flash memory instead using the progrmem option.

A quick google on Arduino Strings had a link to someone dealing specifically with an http client app that was causing him grief because of the size of the strings.

Thanks everyone, then I won't bother digging through the libraries. For now I've left off the SD card parts of the program and that fits, but I may have to go to a MEGA soon anyway.

thanks, Eli

To regain program space on a recent project I dropped the SD card logging as well. Since I had another Arduino around I just turned it, with the SD card shield, into a logger. My project outputs data samples via serial prints and the other Arduino reads them and saves them, to the SD card. It's fairly simple to do.

I also made it so that when you're done logging you can just jumper a pin to ground and it will allow displaying or deleting the file and a few other useful things. Pull the jumper and it's a stand alone data logger.

The SD library code is large but its RAM requirement is also large and I presume that that is what you are really running into. As Jimmy60 has pointed out, one way to get around it is to put just the SD part in another Arduino. Another way would be to replace the SD in the original sketch with an I2C EEPROM and store the data in a raw form. Then write a separate sketch which extracts that data in its verbose form as and when required. You'd just have to figure out what size of EEPROM you would need to hold sufficient data so that the data extraction doesn't have to be performed too often.


Yes, you can shrink libraries, by removing from the .cpp file all those functions that are never called and are never called elsewhere in the library either. The linker won't remove those functions, all it will remove is any entire .cpp files in the library that don't contain any code or data that is needed. So if the library has only a single .cpp file, then you get all of it or none of it.

Once you've removed any functions you don't need, you can remove any data declarations that were used only by those functions.

There are a couple of exceptions:

  1. Templates functions and classes. For these, the compiler only instantiates what is needed; so there is nothing to be gained by removing them.

  2. Functions in derived classes that override virtual functions with the same name and parameters in an ancestor class. In this case, if objects of the derived class are created, then the overriding function can get called even if you think you are calling the version in the base class. The classic case if the write() function in classes such as HardwareSerial that derive from class Print.

Having said that, if you are 5K over the size limit, then it's probably simpler to upgrade to a Mega.