How to query free memory (heap, stack) on RAK Wisblock4631

Hello Everyone,

can you please suggest how can I query the free memory (heap, stack) on RAK Wisblock4631 during runtime? Something similar I thought of like at: Arduino Playground - AvailableMemory

When trying that I have found error during compilation: " undefined reference to `__brkval’ " so came to the conclusion this library won’t work with Wisblock.

(My eventual goal with this is to rule out the cause of a random freeze of a modified version of the GPS tracker solution, altough root cause may as well be because of loosing LoRa connection I guess)

Thank you in advance for any ideas.

Hello @gaborgluck

Heap and stack in the nRF52 are managed by FreeRTOS, the library you tried to use is for AVR MCU’s.


	Serial.printf("Heap total %d used %d free %d", dbgHeapTotal(), dbgHeapUsed(), dbgHeapFree());
	Serial.printf("Stack total %d used %d", dbgStackTotal(), dbgStackUsed());

It shows something like:

But keep in mind, that is the heap and stack assigned to the task that is asking. As FreeRTOS is a multitasking system, each task in the system has its own memory heap and stack size.

Just run a test with an app that has a separate task running and it shows me the same from both tasks.

You can as well try dbgMemInfo();
It gives me the following output:

20:03:45.998 --> | Name | Addr 0x2000xxxx | Usage               |
20:03:45.998 --> | ---------------------------------------------|
20:03:45.998 --> | Stack| 0xF800 - 0xFFFF |   736 /  2048 (35%) |
20:03:45.998 --> | Heap | 0xCDD8 - 0xF7FF | 35276 / 207400 (17%) |
20:03:45.998 --> | Bss  | 0x6000 - 0xCDD7 | 28120               |
20:03:45.998 --> | SD   | 0x0000 - 0x5FFF | 24576               |
20:03:45.998 --> |______________________________________________|
20:03:45.998 --> 
20:03:45.998 --> Task    State   Prio  StackLeft Num
20:03:45.998 --> -----------------------------------
20:03:45.998 --> loop   .X.1.530.1
20:03:45.998 --> IDLE   .R.0.29.3
20:03:45.998 --> LORA   .B.2.3976.8
20:03:45.998 --> BLE    .B.3.1231.6
20:03:45.998 --> SOC    .B.3.163.7
20:03:45.998 --> Tmr Svc.B.2.154.4
20:03:45.998 --> usbd   .B.3.132.5
20:03:45.998 --> Callbac.B.2.729.2

Thank You Beegee for lightning fast answer as always!

Thanks for the pointer to these Bernd. I’ve been hacking at my own implementation of a stack and heap tracer.

RE: the dbgStackTotal() and dbgStackUsed() API, these seem to be reporting the stack stats using __StackTop/__StackLimit which looks to be the stack used by the SoftDevice and ISR, according to a comment in “cores/nRF5/linker/nrf52_common.ld” where they are defined. Which tracks your “same for both tasks” comment.

As you mentioned the individual “task” stacks are managed from the heap by the RTOS when the task is created.

The Stack Addr/Usage listed in the summary of dgbMeminfo() also is derived from the above two dbgStackXXX() API.
The output for the individual tasks seems to track ok though, so that’s good.
I’m dumping extra stack address info (start, next) but that did require modifying the RTOS source, which is not good. I’ll see if can make use of the underlying API that dbgMemInfo() uses, perhaps that will eliminate/reduce the customizing.

Thanks again for pointing out these debug API.