Unused/Unlinked Code using SRAM

I am working on a rather large codebase on a MEGA2560, specifically battling memory usage, and noticed something pretty odd I am hoping someone can explain to me.

i removed usage of a section of code - but not actual code (I use cpp files, so this was a whole directory of files). Predictably the compiled program size and SRAM usage dropped significantly. But then I removed the directory containing the unused source files and... I see another small drop in program size, AND a very significant drop in SRAM usage. Now, I expect the compiler to not link the unused code, but I suspect there is some data is stored in program code about all code compiled, but why is SRAM being used? And we are talking about a pretty significant chunk of SRAM - like 500+ bytes (which is a lot of memory on 8k system)

Can anyone explain to me what is going on? I am missing something about how compiler/Arduino uses ram

Are there any good references to SRAM usage by Arduino - what sort of things to avoid/look for when trying to reduce SRAM usage?

Thanks,

-HH

Here’s the definitive tutorial: https://www.arduino.cc/en/tutorial/memory

The compiler is frighteningly good at removing un-used code. If you don’t call a function then it won’t be linked into the final binary image. But it is possible to confuse it, if you try really hard.

Thanks. I do try really hard ;)

Like I said, what is really freaking me out is that unused code is using up so much SRAM. Not something I would expect - leading me to suspect I do not understand how this works as well as I thought (which was not well to begin with)...

Thanks for the link, but I am looking for something a little more in-depth. I am trying to understand how using C++ things like, for example, classes, enums, etc are affecting SRAM usage. For example, are c style string constants - are they always residing in SRAM or do they get loaded into SRAM when the code using them is run - or what is the difference between the F() macro and PROGMEM char arrays and PSTR macro? Things of that nature.

-HH

I thought it was pretty clear in the tutorial.

The F() macro is a convenient way to access PROGMEM for constant strings. It's not complex.

Enums usually don't get into SRAM. They are constants. The compiler replaces constants throughout the program so that they never use any SRAM at all.

Classes? Well, that depends on the class. You can do stupid things like declaring a large array of data in a class.

:-( Sadly what you said was my understanding as well, but it does not explain why something in the code manages to use up SRAM despite not even being used... Something there must be allocating SRAM on load :-/ I was hoping to find some deeper information about this.

-HH

I was hoping to find some deeper information about this.

All we can see is you waving your hands. Show your code and proof that the memory usage changed.

PaulS: All we can see is you waving your hands. Show your code and proof that the memory usage changed.

Sorry. I asked two separate things in one post and I feel like things got confused here. As for memory usage question, it is non-trivial to post code here as I am unclear where in the code the issue is and we are talking about over 10k lines of code - not to mention the fact that I doubt my client would appreciate me posting their project online.. I realize that I should just re-create this as a proof of concept and post that, but that would require time I do not have at the moment(I will do that soon though). I also realize that without that I am not likely to get help (and reasonably so) - which is why I asked my second question of where I may find some more in depth documentation - as what I found so far is too surface for my needs and my knowledge on the subject is about 20 years old (what remains of it after 20 years of non-use)- but it also occurs to me that what I am asking is probably more gcc/c++ specific than Arduino specific - so I should do some research on that. I guess I was just (unreasonably) hoping there are people here who would know exactly what I am talking about and could immediately point me in the right direction....

Thanks. Sorry for wasting your time :-)

-HH

hichhiker: :-( Sadly what you said was my understanding as well, but it does not explain why something in the code manages to use up SRAM despite not even being used... Something there must be allocating SRAM on load :-/ I was hoping to find some deeper information about this.

-HH

If you want to see where the RAM goes, generate a linker map, and see for yourself. Nobody here can possibly do anything but make wild guesses without seeing the code, and generating a linker map.

Regards, Ray L.

RayLivingston: If you want to see where the RAM goes, generate a linker map, and see for yourself. Nobody here can possibly do anything but make wild guesses without seeing the code, and generating a linker map.

Regards, Ray L.

Thanks, that is very helpful. I forgot that was an option.