Can anyone share techniques or best practices for memory management in uC/OS-III
I'm working on a uC/OS-III application for a Microchip SAMD21 microcontroller that allocates memory for fixed-size buffers used for sensor data. The SAMD21 only has 32KB SRAM, so I'm facing memory fragmentation, which is causing issues with allocating new buffers later in the program.
Can anyone share techniques or best practices for memory management in uC/OS-III, specifically to avoid fragmentation when dealing with fixed-size buffer allocation on a resource-constrained MCU? @Middleware & OS
3 Replies
First of all how you were able to check that heap is getting fragmented
Btw to stop heap from fragmentation you can use deterministic approach where each block of memory allocated is of pre determined fixed size
And is popped out in sequence
2nd if us/os -lll allows static allocation of functions then use that because it will allocate memory at compile time
This is from the uC/OS user's manual:
"Stack space may be allocated dynamically using the C compiler's heap management function (i.e.,
malloc()
... However, care must be taken with fragmentation. If creating and deleting tasks, the process of allocating memory might not be able to provide a stack for the task(s) because the heap will eventually become fragmented. For this reason, allocating stack space dynamically in an embedded system is typically allowed but, once allocated, stacks should not be deallocated. Said another way, it's fine to creae a task's stack from the heap as long as you don't free the stack space back to the heap."
- https://www.analog.com/media/en/dsp-documentation/software-manuals/Micrium-uCOS-III-UsersManual.pdf, page 81
So, are you deallocating stack space?https://percepio.com/tracealyzer/download-tracealyzer/ idk how you were checking the heap but there are free versions of Percepio Tracealyzer for uC/OS that could help if you aren't using something similar already.