Help to interpret memory usage: remain unchanged while defining larger variables

king_griffin

New member
Hi everyone,

I am doing my project with Teensy 4.1 and I need to report the memory usage with respect the size of the matrix variable. I am using Eigen right now.

The simple script is like this (there are more actually)
Code:
typedef Matrix<float, NSTATES, 1> my_VectorNx;
my_VectorNx[2] vector_a;

I used that variable through the program. However when I increased the NSTATES, the total size of my build did not increase, that was not expected.
Before
Code:
teensy_size:   FLASH: code:329560, data:68156, headers:8808   free for files:7719940
teensy_size:    RAM1: variables:74048, code:327048, padding:632   free for local variables:122560
teensy_size:    RAM2: variables:12416  free for malloc/new:511872
After
Code:
teensy_size:   FLASH: code:329240, data:68156, headers:9128   free for files:7719940
teensy_size:    RAM1: variables:74048, code:326728, padding:952   free for local variables:122560
teensy_size:    RAM2: variables:12416  free for malloc/new:511872
This was in Debug mode without LTO, they were actually identical in Release mode. I don't understand the individual size and why individual size changes but not total size.
Could you help me to figure out why this happened?
Thank you very much!
 
I see your point. We define that variable in the main() function, does it count as dynamic allocation?
What will happen if we define them in the global scope?
 
Based on the link the allocation is always dynamic as p#1 shows.

The link shows making an explicit memory allocation and passing the pointer to the library.

Otherwise it seems the reserved space is just a pair of pointers to the memory the library will create.

... just putting that together not having looked at the code but seeing the 'template' provided doesn't do a static/compile time allocation.
 
Before answering, I want to mention the memory chart from the Teensy 4.1 page.

teensy41_memory.png
 
We define that variable in the main() function,

Unless it's declared static, variables within functions get allocated in the top of RAM1. You can see on the chart "Local Variables" with an arrow pointing downward. As each function runs, more of that memory is taken. When a function finishes, its memory is considered unused. If you call another function, and if it calls even more functions, each takes a piece of that top portion of RAM1 while it is executing. This is often called "stack", as if each function were adding another plate onto a stack of plates in your kitchen.


does it count as dynamic allocation?

Normally dynamic allocation refers to use of malloc() or C++ new. You can see that memory indicated in RAM2 on the chart. Each chunk of memory remains allocated until free() or C++ delete is used. This is often called "heap", because, well, I'm not quite sure why. Maybe others can comment on that?


What will happen if we define them in the global scope?

Global variables (and global scope C++ instances), and local variables which are static, are allocated in RAM1. They will either be in the Zeroed Variables area if you didn't get them an initial value or initialized them with zero. If you initialize with a non-zero value, they get allocated in the Initialized Variables area. It's a minor distinction, and hopefully you can see in the chart that all the initialized variables need to have a copy of their initial data in flash which gets copied into RAM1 before your program runs.


Hopefully the memory chart helps?
 
Opps - wrong direction based on p#1 snippet - assumed it was in global scope - wondered but ignored p#3 note Paul picked up on
 
Back
Top