8 years, 2 months ago.

bin file size

The standard nucleo blink example having 12 lines of code (empty lines inclusive) and making use of #include "mbed.h" on the mbed compiler has Flash 14.8 kB and RAM 0.4 kB.

I wrote a program with own classes having in total like 1500 lines of code (empty lines inclusive) plus the SDFileSystem lib and making use of the

  1. include <string>
  2. include <vector>
  3. include <ctype.h>
  4. include <DirHandle.h>
  5. include "mbed.h"

results in Flash 60.8 kB and RAM 4.4. kB.

Now i do not think i have written more than the mbed developers itself plus the code above which the mbed itsself is based (like cmsis) so

1 - how come that my code is so big?

2 - are there any tricks on how to compile the same code resulting in smaller .bin file?

Thnx

2 Answers

8 years, 2 months ago.

Your compiled file size has little to do with the number of lines your code has. For example const int test[4000];, takes 4000 times more flash memory than const int test; does. Ideally you should have alot less flash used if you aren't using any of the functions, but some standard C stuff is always included which makes it cost more memory than what is really needed.

In general, the big stuff is mainly the C libraries (such as stdio, but also vector. Vector is huge if I remember correctly, something like 20-30kB). The mbed library for just your target is actually not all that large. The STM library code which is also included is alot larger already, but if you don't use it, the compiler does not include it in your bin file.

Accepted Answer

Thanks Erik. Do you know of any alternatives to vector that are smaller? i do not want to take care of memory allocation.

posted by anteo c 23 Jan 2016

You can use arrays, which is what is done most often. The alternative is writing your own vector lib which does what it needs to do for you. Of course then you need to also handle the memory allocation, but you only need to do it properly once.

posted by Erik - 23 Jan 2016
8 years, 2 months ago.

Hi Anteo,

"string" standard class eats about 14kB that was about 25% of my project.

My solution was to create my own class with the very minimum set of string functionality I needed using "light" standard C functions (basically strncmp) and pointer manipulation, and other function members I wanted.

I hope that helps!

Thanks Manel for pointing out the size of "string" which i did not know. For the moment i will continue using it and hope that the size will not become bigger since i think i have all includes that i need and i have to just write the lines for which Erik says it should not be a problem.

posted by anteo c 23 Jan 2016