# How can I determine how much memory a function occupies?

How can I view how much program memory a function is occupying ?

I am currently at 95% program memory usage and do not have a licence to enable optimization. I am looking to find out which functions are largest, so I can see if I can perform any manual optimizations there.

I believe I found a way to view how big variables are, by looking at the *.lst file and spotted some things that can be done there.

Compiler is gcc based (Microchip XC8).

• This might be easier to answer if you give some information about your toolchain. IDE, compiler, hardware. Can you look at the assembly produced by the compiler? – Jack B Apr 22 at 20:14
• @JackB It's more of a generic question but I will add that its a gcc based compiler (Microchip XC8 specifically) but I am hoping that the information can be applied to all gcc based outputs. And yes I have access to all output files gcc generates, lst, map, hex, elf, sym etc.. – efox29 Apr 22 at 20:21
• Try using the linker to dump out a map file (I think using the -Map option.) Also consider using objdump, too. Just go look up "map file" and "gcc" on the web. Different toolchains have different methods and different capabilities. So there isn't a universal fits-all answer. You can also ask it to generate the assembly. But I'd start with a detailed map file, I think. Also, I don't know how carefully they separated out the optimizations, but adding features to GNU toolsets may mean they provide the source code somewhere and if you can go find it and set up the tools, you can gen optimization. – jonk Apr 22 at 20:24
• @jonk will give this a go! – efox29 Apr 22 at 20:32
• @ThePhoton I did - Microchip XC8 (gcc based) – efox29 Apr 22 at 23:23

I spent some time digging into the map file (which was also a suggestion by @jonk), and it looks like function sizes are available in the file.

For anyone interested, if you look inside a map file (in this case a XC8 generated one)

Name             Link     Load   Length    Selector   Space Scale
text3             41       41      195        8           0


The length of text3 @ address 0x41 is 0x195 bytes in length.

Elsewhere in the map file, you'll find the symbol table and will find what function is linked to text3.

_foo_open    text3        00041


When I compile program with foo_open() my program size is 1878 byes (0x756) and when I completely remove foo_open() my program size is 1472 bytes (0x5C0).

The difference between is 0x196. I am off by a 1 (not sure where/why) but I think this is close enough to what I am looking for.

So going even furthur into this, there is a section called MODULE INFORMATION, that tells you how big your module is, as well as each function.

Module    Function      Class          Link    Load   Size
foobar_driver.c
_clear_bar      CODE            0729    0000    7
_display        CODE            0583    0000    21
_set_bar        CODE            032A    0000    56
_set_foo        CODE            02F3    0000    56
_turn_off       CODE            0761    0000    5
_clear_foo      CODE            0723    0000    7
_toggle         CODE            05BD    0000    20

• Your off by 1 could be padding added by the linker to ensure functions are on a word aligned boundry – Colin Apr 23 at 19:12
• @Colin fair point – efox29 Apr 23 at 19:14

You can always comment out the function call and re-compile. Your compiler will optimize out the unused function definition, and you'll see what difference it makes on your program memory. It's a quick and dirty way to compare the memory weight of different functions in your program.