Defined in stdlib.h, the
__heapstats() function displays statistics on the state of the storage allocation heap.
The default implementation in the compiler gives information on how many free blocks exist, and estimates their size ranges.
__heapstats() function generates output as follows:
32272 bytes in 2 free blocks (avge size 16136) 1 blocks 2^12+1 to 2^13 1 blocks 2^13+1 to 2^14
Line 1 of the output displays the total number of bytes, the
number of free blocks, and the average size. The following lines
give an estimate of the size of each block in bytes, expressed as
__heapstats() does not give information
on the number of used blocks.
The function outputs its results by calling the output function
that must work like
fprintf(). The first parameter
dprint() is the supplied pointer
You can pass
fprintf() itself, provided you
cast it to the right function pointer type. This type is defined
typedef for convenience. It is called
NoteIf you call
fprintf()on a stream that you have not already sent output to, the library calls
malloc()internally to create a buffer for the stream. If this happens in the middle of a call to
__heapstats(), the heap might be corrupted. Therefore, you must ensure you have already sent some output to
If you are using the default one-region memory model, heap memory is allocated only as it is required. This means that the amount of free heap changes as you allocate and deallocate memory. For example, the sequence:
int *ip; __heapstats((__heapprt)fprintf,stderr); // print initial free heap size ip = malloc(200000); free(ip); __heapstats((__heapprt)fprintf,stderr); // print heap size after freeing
gives output such as:
4076 bytes in 1 free blocks (avge size 4076) 1 blocks 2^10+1 to 2^11 2008180 bytes in 1 free blocks (avge size 2008180) 1 blocks 2^19+1 to 2^20
This function is not part of the C library standard, but the ARM® C library supports it as an extension.