2

Upon compiling a sketch using the Arduino IDE, a message like the following gets displayed in the console:

Global variables use 1540 bytes (75%) of dynamic memory, leaving 508 bytes for local variables. Maximum is 2048 bytes.

In addition, it seems that if the global variables use exceeds around 75% of total dynamic memory, the following message gets displayed in addition:

Low memory available, stability problems may occur.

So, it seems that by using the IDE in a regular way, the compiler does not ensure that local variables memory allocation won't cause any RAM overflow during program execution. Instead, it just displays a warning message when little RAM is left for local variables.

This can lead to program malfunctions at execution time, if local variables happen to take up more space than the remaining RAM.

How can one have the compiler detect whether local variables memory allocation will cause RAM overflow during execution or not, and warn the user at compile time only in those specific cases?

9
  • 1
    the compiler knows nothing about the program's logic
    – jsotola
    Commented Jun 7, 2022 at 17:09
  • How do you expect the compiler to guess what might happen in your program that relies on external stimulii to control its operation?
    – Majenko
    Commented Jun 7, 2022 at 17:11
  • 1
    and why do you ask it here on Arduino SE?
    – Juraj
    Commented Jun 7, 2022 at 18:03
  • 1
    The underlying GCC might have an option to check during runtime whether the stack runs into used RAM, depending on its version. But I have no idea how to add such a potential option in the Arduino IDE. Please note that it slows down your sketch, because this check is most probably done each time the used stack space increases, that is at function/method calls or local variable allocations. Commented Jun 8, 2022 at 5:47
  • 1
    Look at it like this. If you have 2kB of RAM and 2 functions, a() and b(), each of which dynamically allocate 1.5kB, you may be OK. It depends. It is definitely bad luck if b() calls a(), a() calls a() etc. If a() and b() are never active simultaneously it may work. If b() calls a() depending on user input at runtime, the compiler can't see that.
    – 6v6gt
    Commented Jun 9, 2022 at 10:31

1 Answer 1

2

You are describing "memory profiling" and is not typically a feature of C or C++. It is more common in higher level languages, such as Java, but C is a considered a much lower level language.

Typically such profiling is done at runtime using external tools such as valgrind and massif.

It's not really possible for the compiler to work out what your program will be doing when it is using external data and inputs to control how it works. Any such tests at compile time would be pretty much meaningless since any that pass in testing may still cause problems at runtime depending on what else may be happening at the same time, or even what may have happened in the past (see heap fragmentation).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.