Check heap usage at runtime

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

Check heap usage at runtime

993 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MichelKuenemann on Sun Mar 29 22:06:31 MST 2015
Hi all,

I use dynamic memory allocation in my applications, and I need to check the heap usage at runtime.

This link gives good information to set the heap start and limit: http://www.lpcware.com/content/faq/lpcxpresso/heap-checking-redlib

I would also like to read the  "__end_of_heap" variable to check the available memory, but the linker does not find this symbol:


printf( "End of heap %x\r", __end_of_heap);

error: '__end_of_heap' undeclared (first use in this function)

How can I fix this issue ?

Thanks a lot

Regards,
Michel
0 Kudos
8 Replies

714 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MichelKuenemann on Wed Apr 01 12:30:25 MST 2015
Hello,

Yes, that's clear. it is not an issue in my case because, in my applications, I (almost) never free() memory :-)

I think that this topic can be closed now.

Thanks a lot for your time.

Regards,
Michel
0 Kudos

714 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Wed Apr 01 05:09:28 MST 2015
Yes as long as you remember that the location pointed to by __end_of_heap includes the space occupied by free'ed blocks at the end of the heap.

Regards,
LPCXpresso Support
0 Kudos

714 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MichelKuenemann on Tue Mar 31 06:08:00 MST 2015
Hello,

If I compute the available dynamic memory this way, am I correct ?

unsigned long  available_dynamic_memory;

available_dynamic_memory  = _pvHeapLimit - __end_of_heap;


Regards,
Michel
0 Kudos

714 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Tue Mar 31 04:42:16 MST 2015

Quote: MichelKuenemann

extern void *_pvHeapLimit; // To be confirmed.



_pvHeapLimit is the location that the current end of the heap is checked again when more memory is requested. If the additional memory required would take the heap beyond _pvHeapLimit, then malloc will fail.

Please re-read the FAQ for more details:

http://www.lpcware.com/content/faq/lpcxpresso/heap-checking-redlib

Regards,
LPCXpresso Support
0 Kudos

714 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MichelKuenemann on Tue Mar 31 02:17:51 MST 2015
Hello,

Thank you for these informations.

Additionaly, I have tried following :

 extern void *_pvHeapLimit;

 printf("_pvHeapLimit = %x\n", (unsigned int) _pvHeapLimit);


I did not test it at run time, but it compiles.

This variable seems to be the "heap limit" I was looking for.

So finally, follwowing variables are available :

extern void *__end_of_heap;
extern void *__heaps;
extern void *_pvHeapStart;
extern void *_pvHeapLimit; // To be confirmed.


Regards,
MK
0 Kudos

714 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Mon Mar 30 05:52:42 MST 2015

Quote: MichelKuenemann

Is there a global variable containing the start of the heap ?



You should be able to do this using either:

   extern void *__heaps;
     :
    printf("__heaps = %x\n",__heaps);


or
    extern void *_pvHeapStart;
      :
    printf("_pvHeapStart = %x\n",&_pvHeapStart);



Quote: MichelKuenemann

Is there a global variable containing the limit of the heap ?


This is what __end_of_heap is, but it includes free'ed blocks at the end of the heap, which may not what you are looking for.

Regards,
LPCXpresso Support
0 Kudos

714 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MichelKuenemann on Mon Mar 30 04:20:06 MST 2015
Hello Support,

Thank you for the info. It works fine now.

I have some other questions regarding the run time management of the heap:

Is there a global variable containing the start of the heap ?
Is there a global variable containing the limit of the heap ?

My goal is to create a function that returns the total size of the heap and the current heap consumption, a bit like a fuel gauge.

Thank you.
Regards
Michel
0 Kudos

714 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Mon Mar 30 00:52:51 MST 2015
Adding the following declaration to your source should allow the link to suceed:

extern void *__end_of_heap;


Note that by default, printf uses the heap too, so you might want to switch to the character output (non-heap) version, as described in:

http://www.lpcware.com/content/faq/lpcxpresso/using-printf

Finally, note that in the current Redlib heap functions, __end_of_heap basically does mark  the maximum extent that the heap has reached.

Regards,
LPCXpresso Support
0 Kudos