MemoryUsage
Arduino library MemoryUsage
MemoryUsage Documentation

A full explanation in french can be read at http://www.locoduino.org/ecrire/?exec=article&action=redirect&type=article&id=149 .

Roughly, the SRAM memory is divided into four areas: the static data, the heap, the free ram and the stack.

The static data size is given by the compiler itself after the building. this is filled by all variables and arrays declared in global scope, or with 'static' keyword.

The heap is filled with all the dynamic allocations done with 'new' keyword or 'malloc' functions.

The stack start from the end of the SRAM area and grow and shrink downward at each function call, it stores all the local data internal to a function, function arguments (depending of the architecture, arguments can be stored in CPU registers to improve speed...) , and addresses for function returns to caller.

SRAM memory

+---------------+------------------+---------------------------------------------+-----------------+
|               |                  |                                             |                 |
|               |                  |                                             |                 |
|    static     |                  |                                             |                 |
|     data      |       heap       |                   free ram                  |      stack      |
|               |                  |                                             |                 |
|               |                  |                                             |                 |
|               |                  |                                             |                 |
+---------------+------------------+---------------------------------------------+-----------------+
       _end or __heap_start     __brkval                                         SP             RAMEND

Source : http://www.nongnu.org/avr-libc/user-manual/malloc.html

MemoryUsage try to help you to find the actual memory status with differents strategies, but dont forget that when you observe something, you change the result of the observation : execution time is consumed by the analysis tools, and memory used will grow because of these tools !

  1. First, there are the MACROs to show memory areas start/end addresses and actual sizes.
  2. Second, there is a display function to show a 'map' of the memory...
  3. Third, a function can give you the current size of the free ram using a stack tag, which is more accurate than the MACRO.
  4. Fourth, an elegant way to try to understand how much size has reached the stack during execution. It will 'decorate' the internal memory, and try to identify after a moment of execution at what place the first byte of the memory is not anymore decorated... The function mu_StackPaint will be called _before the setup() function of your sketch, to 'paint' or 'decorate' all the bytes of the SRAM momery with a particular code, called the CANARY... Later, a function mu_StackCount can be called to get the actual maximum size reached by the stack by counter the byte no more painted. This is a copy / adaptation of the library StackPaint available here : https://github.com/WickedDevice/StackPaint
  5. And five at least, and because a stack grow and shrink continuously, the macros STACK_DECLARE / STACK_COMPUTE / STACK_PRINT try to get the greatest size of the stack by 'sampling' the execution. Start your code by
    #include <MemoryUsage.h>
    
    STACK_DECLARE
    
    void setup()
    ...
    

then add a STACK_COMPUTE in any function that can be called :

void subFonction()
{
    double v[SIZE];
    STACK_COMPUTE;

    .... // do things
}

and finish by printing on the console the biggest size of the stack with STACK_PRINT or STACK_PRINT_TEXT. Be careful with this method, this introduce some code in every function of your sketch, so if the timing is important for your applicaion, take care of it !