MQX - How to determine amount of "heap" remaining?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MQX - How to determine amount of "heap" remaining?

834 Views
dmarks_ls
Senior Contributor I

Simple question... is there a way to determine how much memory remains in the default system pool during runtime?  I've read through the MQX Reference Manual, but I can't seem to find a relevant function for determining how much system memory remains.  For reference, I'm allocating memory with _lwmem_alloc_system(), and freeing with _lwmem_free().  We're running up against memory limitations in our current project, but the only way we know this is when a task fails to launch (because MQX could not allocate stack for it) or when MFS fails to initialize (because there isn't enough memory to init the filesystem).  It would be extremely useful to know how much memory remains without having to tweak numbers and recompile until something works.  Thanks.

David

Tags (4)
0 Kudos
1 Reply

280 Views
Martin_
NXP Employee
NXP Employee

Hi David, the amount of free memory at one point at time is typically a dynamic value. Even if you had such a function, it might return you a value that won't be true one instruction opcode later, as an interrupt might occur, reschedule, other task allocates or frees resources ....

The only way is to try to allocate what you need (this is atomic operation) and check return value.

there is highwater mark (TAD can read it, or it can be read from source code: _mem_get_highwater()) that will give you the highest memory address that MQX has allocated in the default memory pool. Note this value is not exactly the amount of free memory at a time. There can be more free memory than (end-of-SRAM minus highwater mark).

Martin

0 Kudos