9

When I compile my code, the Arduino IDE returns the binary sketch size in byte.

Is there a good way to find out (approximately) what function or what part of my code takes up how much memory in flash, so that I get a feeling what feature takes most of precious flash memory or where I may be wasting flash and should start optimizing?

6

One approach is to look at the assembly listing of the program.

  1. After compiling the program (via the Verify or Download button) locate the directory where the .cpp, .hex, and .elf files for the program have been stored. (If you have trouble finding them, briefly turn on verbose compile options in the arduino IDE and look at the directory names in the output.)

  2. In that directory, run avr-objdump -S fn.cpp.elf > listing.txt where fn.cpp.elf is the name of your sketch with .ino taken off and .cpp.elf added on at the end. For example, with sketch serialecho.ino:

    avr-objdump -S serialecho.cpp.elf > listing.txt

  3. View the listing.txt file, and subtract each function's starting address from its ending address, using hex arithmetic. For example, with setup code

    void setup(){ Serial.begin(9600); }

in the listing we will see

00000144 <setup>:
...(6 lines snipped)...
 144:   26 e0           ldi     r18, 0x06       ; 6
 146:   40 e8           ldi     r20, 0x80       ; 128
 148:   55 e2           ldi     r21, 0x25       ; 37
 14a:   60 e0           ldi     r22, 0x00       ; 0
 14c:   70 e0           ldi     r23, 0x00       ; 0
 14e:   80 e1           ldi     r24, 0x10       ; 16
 150:   92 e0           ldi     r25, 0x02       ; 2
 152:   db c1           rjmp    .+950           ; 0x50a <_ZN14HardwareSerial5beginEmh>

00000154 <loop>:

The calculation 0x154–0x144 = 0x10 shows this portion of setup is 16 bytes long.

These examples were generated on a linux system. I think the approach is the same on a MSWindows system except you may need to say avr-objdump.exe instead of avr-objdump, and may need to specify its path. See, eg, the Assemler results, where? thread at forum.arduino.cc.

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.