I have a lot of global variables and I don't know if they are all necessary so it would be good if I could see them listed by size to see which are having the biggest effect.
In C++ terms, I think I would need to be looking at the bss and data sections with an elf but I might be going down the wrong tracks.
Sorry, this won't be the answer you want. This is really bad software engineering. You should restructure and/or document your code so there isn't a mystery about whether they are necessary. You can see where they are referenced by doing a global text search on the identifier.
If you have too many global variables, look to see if they all have a good reason to be global. If not, move them inside your functions and make them static if necessary.
Adjusting the compiler warning level in Preferences can help identify such unused definitions.
But, as has been said, it is time to consider restructuring your program, so it is easier to have an overview of it all. This includes using functions and keeping data local to the functions which use it, grouping functions in separate files (.cpp and .h files), possibly using classes to encapsulate functions (methods) and data, maybe using namespaces etc.
aarg:
If you have too many global variables, look to see if they all have a good reason to be global. If not, move them inside your functions and make them static if necessary.
That's not going to have any impact on the amount of SRAM needed although it will help to satisfy the diktats of the programming purists.
If a global variable is defined in a piece of code but not used anywhere the compiler won't include it in the code and it won't take up any SRAM.
When I am re-organising a program and think I have no need for a variable that I had defined previously I just comment-out the definition and re-compile the program. The compiler will then tell me all the places where that variable is referenced.
If you have too many global variables, look to see if they all have a good reason to be global. If not, move them inside your functions and make them static if necessary.
totally agree. Use globals only if global scope is needed.
Robin2:
That's not going to have any impact on the amount of SRAM needed although it will help to satisfy the diktats of the programming purists.
. . .
I don't think that the issues referred to by the OP offend, exclusively, the sensibilities of programming purists. Common sense dictates, that if you have developed a program in such a way, that you have lost the overview over it, that some remedial action is necessary.
6v6gt:
if you have developed a program in such a way, that you have lost the overview over it, that some remedial action is necessary.
I couldn't agree more that remedial action is necessary when the overview is lost.
The real problem is that we don't know enough about the way in which the OP has lost his overview. Maybe a complete re-write is what is needed. Or maybe it's a case of "if it works don't fix it"
Thanks for all the comments. I don't think my code is as bad as everyone is implying. I have allegedly 300kb of "global variables" (which I believe includes static variables in functions and files in .cpp as well). This seems higher than what it should be.
I have no real need right now to bring it down as I have about 200kb left and an 8MB PSRAM chip coming (I am using a Teensy 4.1) but it would be nice to have cleaner code.
I used What's Using My Arduino RAM and FLASH (thanks 6v6gt) and it showed about 65kb instead of 300kb, though the tool is intended for AVR and not ARM, but I don't think that should make a difference. Perhaps the 32 bit ARM chip stores variables as four bytes instead of one? Nobody will ever know.
In 35+ years of c/c++ programming, I don't think I've EVER had a single application with anything CLOSE to 300K of global data. I cannot imagine that makes sense in a properly-structured program....
I am using the Arduino IDE definition of global variables, which I think means all the memory allocated at compile time. Have I misunderstood something?
RayLivingston:
In 35+ years of c/c++ programming, I don't think I've EVER had a single application with anything CLOSE to 300K of global data. I cannot imagine that makes sense in a properly-structured program....
Two or three large arrays could easily account for it. And they may be easier to work with if they are defined as global.
"global" and "allocated at compile time" do NOT mean the same thing. Variables can be declared static, which means they are defined at compile time, but they are NOT necessarily global. "global" defines the scope of the variable - WHERE it is visible. A global variable is visible to ALL parts of the program, while static variables can be visible only to a very few lines of code, to a specific block of code (between a pair of { & }), to a specific function, class, file, etc. It all depends where and how the variable is defined.
Google "c variable scope". It is a somewhat complex, but absolutely critical, concept to master.
Robin2:
Two or three large arrays could easily account for it. And they may be easier to work with if they are defined as global.
...R
No doubt, but good program structure would still dictate restricting their scope to only on those parts of the code that actually NEED direct access. It is, arguably, almost always "easier" to make all variables global, but that is nearly always the wrong choice.