1

For example RTClib library have codes for DS1307, DS3231, PCF8523 and softRTC. but I'm only using DS3231 and don't need the extra codes there to occupy my Arduino flash memory.

Does Arduino IDE only "upload" those parts of libraries that is being used in the sketch or just uploads the whole library?

1

Normally a library supports various kinds of components; because mostly those components share a lot functionality, so it prevents to make very many different libraries.

Note that all functions that you do not call within your program (and are not called from within the library), will NOT end up in your sketch, thus also will not affect your flash memory usage.

If the library is full of if statements to make a difference between models, that code is in (if a function is called, the entire function is stored in flash). (Unless the if statement can be evaluated compile time, see remark of Edgar Bonet below about link time optimization).

So if you want to remove that 'unnecessary' code (for you at least), than copy the library into your project and remove the unnecessary code yourself.

However, I would not do that until you run out of flash memory.

You can do an easy test:

  • Include a library, and compile your sketch. You will see (possibly) some difference.
  • Now call a function from the library. Compile the sketch again, you will see the flash memory usage has increased. This means, before that, that function was not part of your compiled sketch.

A thing I would worry more about, is SRAM memory usage, but mostly buffers are created depending on the model, and SRAM is not wasted (since the Arduino Uno has only 2 KB for example).

  • 4
    Re “if the library is full of if statements to make a difference between models, that code is in”: Since version 1.6.10, the Arduino IDE enables link time optimization, and this is no longer always true. If the conditions in the ifs are compile-time constants, the optimizer notices that some parts of the code is unreachable, and discards them. – Edgar Bonet Jul 19 at 21:04
  • @EdgarBonet Thanks for that remark; I updated my question (credits for you). I didn't knew about this feature. – Michel Keijzers Jul 19 at 21:10
1

The Arduino IDE uses the gcc compiler.

Gcc is an optimizing compiler, and is quite good at it. So basically it's going to do a good job of identifying and including only the functions you actually use in your code.

Unless you are quite experienced you will probably not do a better job than the compiler itself.

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.