Timmmm
Timmmm

Reputation: 97058

How do I see how much RAM global variables use?

In C/C++ in the context of microcontroller programming, how can I see how much memory global variables use (not counting new/malloc-allocated data)?

Upvotes: 4

Views: 823

Answers (1)

Timmmm
Timmmm

Reputation: 97058

If you're using gcc to link your executable / firmware, you can add the option -Map=memory.map to the command line.

In that file you'll find something like this, which tells you where RAM is:

Memory Configuration

Name             Origin             Length             Attributes
FLASH            0x00014000         0x0002c000         xr
RAM              0x20002000         0x00002000         xrw
*default*        0x00000000         0xffffffff

Note the address of the RAM (0x20002000). Later on you'll find the addresses of your global variables in RAM. The difference in addresses will tell you their size:

 .bss           0x20002924       0x94 C:/Users/...../main.cpp.o
                0x20002924                i2c
                0x20002934                ex1
                0x20002948                ex2
                0x2000295c                sensorI2C
                0x20002978                sensorSPI0
                0x2000299c                sdCard

The first column (.bss) indicates that the we are in the data segment where uninitialised global variables are stored. The second column is the address (which by comparing to the above we can see is in RAM. Third column (0x94) shows the total size used by main.cpp, and the final column gives the source file name & the symbols.

Note: Initialised "Plain Old Data" global variables (e.g. int a = 1;) will go in the .data segment, so look there too! (Thanks Paul) Variables that are initialised with C++ constructors still go in .bss. Check both segments anyway.

Upvotes: 3

Related Questions