Go Down

Topic: [SOLVED] Measure free SRAM of Nano 33 BLE Sense at runtime (Read 61 times) previous topic - next topic

arhyrhy123

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.  https://playground.arduino.cc/Code/AvailableMemory/
2.  https://learn.adafruit.com/memories-of-an-arduino/measuring-free-memory
3.  https://stackoverflow.com/questions/8649174/checking-memory-footprint-in-arduino


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.

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.

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

arhyrhy123

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/
This works like a charm. Thanks! 
A slightly improved function for Arduino:
Code: [Select]
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);
}

Go Up