[SOLVED] Measure free SRAM of Nano 33 BLE Sense at runtime

Hi guys,

How can we measure the available SRAM of the Nano 33 BLE board at everywhere in the code at runtime? Or with what tool?

I tried some posts online:

  1. Arduino Playground - AvailableMemory
  2. Measuring Memory Usage | Memories of an Arduino | Adafruit Learning System
  3. Checking memory footprint in Arduino - Stack Overflow

Only the thrid one works to some extend (in small project like blinking). If the code is put into some larger program, the program hangs in the memory measurement function.

Why the first and second methods doesn't work: I guess this is because the Arduino Nano 33 has a different archtecture as other older boards.

What is the most elegent way to check available SRAM at runtime?

Thanks in advance.

The Arduino Nano 33 BLE uses mbedOS and bare metal approach will likely not work. Maybe the following page helps answer your questions.

https://os.mbed.com/blog/entry/Tracking-memory-usage-with-Mbed-OS/

Klaus_K:
The Arduino Nano 33 BLE uses mbedOS and bare metal approach will likely not work. Maybe the following page helps answer your questions.

Tracking memory usage with Mbed OS | Mbed

This works like a charm. Thanks!
A slightly improved function for Arduino:

void print_memory_info(char* printEvent, int iSize) {
    // allocate enough room for every thread's stack statistics
    int cnt = osThreadGetCount();
    mbed_stats_stack_t *stats = (mbed_stats_stack_t*) malloc(cnt * sizeof(mbed_stats_stack_t));
 
    cnt = mbed_stats_stack_get_each(stats, cnt);
    for (int i = 0; i < cnt; i++) {
        snprintf_P(printEvent, iSize, "Thread: 0x%lX, Stack size: %lu / %lu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size);
        Serial.println(printEvent);
    }
    free(stats);
 
    // Grab the heap statistics
    mbed_stats_heap_t heap_stats;
    mbed_stats_heap_get(&heap_stats);
    snprintf_P(printEvent, iSize, "Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
    Serial.println(printEvent);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.