2022-08-30 03:08 AM
Hello,
During code execution I would like to monitor the available memory. Is there a default function available to check the available RAM memory?
Thanks
2022-08-30 03:25 AM
What do you mean by "available memory"?
Normally, memory is allocated during translation (linking) and you can check its consumption e.g. in mapfile.
Runtime, memory is consumed by stack and dynamic allocation (malloc() - heap). The latter is unlikely to be used in STM32L0. Stack pointer can be read out and checked runtime, but that has questionable value, given the check is not guaranteed to happen at the moment when the stack is at its deepest point.
So I'm not quite sure what exactly do you want to check and why.
JW
2022-08-30 03:58 AM
Simple this function exist only on high level OS.
Read somethink c - malloc behaviour on an embedded system - Stack Overflow
2022-08-30 04:59 AM
Thanks for your feedback and links.
The reason why I would like to check memory (RAM) is because I used a external library with a memory leak. I solved the leak but it triggered me to look for a way to measure memory usage.
2022-08-30 05:39 AM
These things typical use simple linked lists with a small chaining structure ahead of the pointer returned by the allocator.
The free memory list typically available as a pointer.
2022-08-30 07:35 AM
Any example or tip? I search on Google but could not find them. Thnx
2022-08-31 09:26 AM
Source to the GNU libraries should be open.
It's just use a linked-list, so perhaps 2 to 4 words in front of the allocated pointer.
uint32_t *p = malloc(1024);
uint32_t *q = p - 3;
printf("%p %p %p\n",q[0],q[1],q[2]);
You'd basically find the pointer to the free list, and then walk the list, should be able to determine the total free space, and the largest ones, and how fragmented the heap is.
Heap and List stuff presumably covered in first year CompSci college texts.