2

I am using an Arduino with 2048 bytes of SRAM. I have a complex project so that it is no longer obvious how much SRAM it would use in total (there is not one large dominant object but many different routines, many libraries most of which I have not written myself etc. Each of these uses some small buffers, global variables etc.).

Can I determine how much SRAM I have left in the live system?

3
  • In the preferences set the compiler to verbose output. You'll get a message like Global variables use 9 bytes of dynamic memory.
    – Gerben
    May 14, 2015 at 16:49
  • It's already set to verbose (Show verbose output during compilation) but there is no such output? May 14, 2015 at 16:55
  • @Gerben: this only tells you the amount of static RAM usage, i.e. the data and bss sections of the memory. When the program runs, it will also consume memory in the stack, and possibly in the heap. You can estimate the amount of free memory by looking at the gap between the heap and the stack, at runtime. May 14, 2015 at 17:02

1 Answer 1

5

Expanding on my previous comment... The RAM memory map of an AVR-based Arduino looks like this:

┌──────┬─────┬──────┬──────────┬───────┐
│ data │ bss │ heap │→  free  ←│ stack │
└──────┴─────┴──────┴──────────┴───────┘

The data and bss sections are fixed, and their size can be known at compile time. However, the stack and the heap grow and shrink dynamically as the program runs. You can estimate the amount of free memory at run time by measuring the gap between the heap and the stack. The result may be pessimistic if the heap contains usable holes.

Here you have:

Your Answer

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

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