Code compilation: what is "padding"?

Not open for further replies.


Well-known member
Hi all,
my application's code is growing and I'm very close to run out the RAM1 memory; this is the last report:

Memory Usage on Teensy 4.1:
FLASH: code:295492, data:28076, headers:8204 free for files:7794692
RAM1: variables:217792, code:292344, padding:2568 free for local variables:11584
RAM2: variables:31104 free for malloc/new:493184

Actually I'm working on reducing/dropping some large or redundant arrays, in order to lower the 217792 value; don't know what to do to control the "padding" value: what is padding and and what causes its growth?
Thanks in advance!
Ram1 which is 512KB is actually something like 16*32kb chunks of memory where each chunk can be configured to be data (DTCM) or Code/Instructions (ITCM)..
So what it saying is you have 292344 bytes of code space... Which will take 8.92 of those chunks for code. So round up to 9... So 9*32768-292344 = 2568...
Which is the space left in the end of that chunk, that is not easily available for data...

As for decreasing memory space... Often times if you have tables like: const uint32_t my_table[]= {.....}
if you make sure it also has the keyword PROGMEM it will leave that memory in FLASH instead of copy down to data and you save you that space.
Likewise if your code uses a lot of string, like: Serial.println("This is some text")... This memory usage can be reduced by using the F()...
Serial.println(F("This is some text")0
Which leaves these strings in flash.

If you have a lot of code that does not need to run at the highest speeds, you should mark those functions (often init routines). with the FLASHMEM
keyword, which will then not copy those functions to ITCM but run them from FLASH. But you would need to mark a lot of it to do this as you are using
about 29+K in the last ITCM chunk, so you would need to reduce the code that is placed in ITCM by that amount to then have that last chunk be given to DTCM.

There are some hints about memory usage up on the product page:
Thank you Kurt, now it's very clear. Also thank you for all these suggestions; I have some large const arrays, now all in PROGMEM, and a lot of not critical functions, that I've moved to FLASHMEM. Now:

FLASH: code:298908, data:28076, headers:8884 free for files:7790596
RAM1: variables:209600, code:280264, padding:14648 free for local variables:19776
RAM2: variables:31104 free for malloc/new:493184

Netx step: I'll measure the execution time of one "critical" function (in ITCM) using a const variable when this const is in DTCM, and when I move it to PROGMEM.
The function examinated parses a bidimensional array:
const char name[256][3]

looking for one element (the last one).

Using T4.1@600MHz and different combinations (as follows) the execution time changes almost negligibly:

Function in FLASHMEM, const array in PROGMEM: 32 microseconds
Function in FLASHMEM, const array in DTCM: 31 microseconds
Function in ITCM, const array in DTCM: 30 microseconds
Function in ITCM, const array in PROGMEM: 30 microseconds
Not open for further replies.