Hi Mark, On Mon, Jan 6, 2020 at 6:08 PM Mark Ruys <mark.r...@sensoterra.com> wrote:
> Is there a way to determine the current maximum heap usage. I want to > check in my application how low free heap became so far to warn if it got > too low. Idem for the stacks. Basically tools like > https://os.mbed.com/docs/mbed-os/v5.14/tutorials/optimizing.html#runtime-memory-statistics > < > https://os.mbed.com/docs/mbed-os/v5.14/tutorials/optimizing.html#runtime-memory-statistics > > > If you just want to track max bytes allocated from stack at any time you'll need to hack malloc/free to track this - it should be fairly simple. > Thanks, > > Mark > > PS This gives me the current free heap: > uint16_t heap_free = &__HeapLimit - (char *)_sbrk(0) > This is not current free heap space - it's just the size of remaining space that heap can use. This is because baselibc can increase program break on malloc, but will never decrease it. However, you can use similar calculation to get something that is closer to what you want: size_t heap_max = (char *)_sbrk(0) - &__HeapBase; This will return max ever bytes used by heap. It's not the same as max ever bytes allocated from heap since there can be some free space included due to fragmentation but honestly it's much better than just raw number of allocated bytes since it allows to estimate actual required heap size. > For stack I iterate os_task_info_get_next(), giving me oti_stksize and > oti_stkusage. Best, Andrzej