Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


Querying free heap memory

Posted by incrediball on April 16, 2008
Now that we've got functions like uxTaskGetStackHighWaterMark for helping us to fine tune the stack sizes, it would be nice to have a function that returns the available heap memory as well. It's presently just guess work unless we patch heap_1.c (or whatever) to find out how much memory has actually been allocated.

RE: Querying free heap memory

Posted by Dave on April 16, 2008
This could be done through some more hook macros that allow you to track the amount of heap taken and returned. I think this was suggested on this forum before. It would not tell you anything about heap fragmentation though.

RE: Querying free heap memory

Posted by Ricky on April 16, 2008
Use this function to walk the heap. It also shows free ROM and RAM. It's written for the STR750 with a version 4 IAR compiler.

#pragma segment = "ROM0_START"
#pragma segment = "ROM0_TOP"
#pragma segment = "ROM0_END"
#pragma segment = "RAM_START"
#pragma segment = "RAM_TOP"
#pragma segment = "RAM_END"
#pragma segment = "HEAP"

typedef struct FREE FREE;

struct FREE { // Heap free block list structure:
uint32_t sulSize; // Free block size
FREE *spsNext; // Pointer to the next free block

extern FREE __data_Aldata; // Heap information structure

void ShowHeap (void) {
FREE *xpsFree; // Free heap block
long *xplStack; // Stack pointer
long xlRomSize; // ROM size
long xlRamSize; // RAM size
long xlMemory;
long xlHeapTotal;
long xlHeapFree;
long xlHeapUsed;

// The heap is walked to find the amount of free memory available.

xlHeapFree = 0;
xpsFree = &__data_Aldata;
while (xpsFree->spsNext) {
xpsFree = xpsFree->spsNext;
xlHeapFree += xpsFree->sulSize;
xlHeapTotal = (long) __sfe ("HEAP") - (long) __sfb ("HEAP");
xlHeapUsed = xlHeapTotal - xlHeapFree;

// Header

printf ("Heap | Free stack | Free memory\r\n");
xlRomSize = (long) __sfb ("ROM0_END") - (long) __sfb ("ROM0_START") + 1;
xlRamSize = (long) __sfb ("RAM_END") - (long) __sfb ("RAM_START") + 1;

// Used heap, FIQ stack, and free flash

for (xplStack = (long *) &Stack_FIQ;
xplStack < (long *) &StackTop_FIQ && *xplStack == RAM_TEST;
xlMemory = (long) __sfb ("ROM0_END") - (long) __sfb ("ROM0_TOP") + 1;
printf (" Used: %5d | FIQ: %5d | Flash: %6d (%d%%)\r\n",
xlHeapUsed, 4 * (xplStack - (long *) &Stack_FIQ), xlMemory,
100 * xlMemory / xlRomSize);

// Free heap, IRQ stack, and free RAM

for (xplStack = (long *) &Stack_IRQ;
xplStack < (long *) &StackTop_IRQ && *xplStack == RAM_TEST;
xlMemory = (int) __sfe ("RAM_END") - (int) __sfb ("RAM_TOP") + 1;
printf (" Free: %5d | IRQ: %5d | RAM: %6d (%d%%)\r\n",
xlHeapFree, 4 * (xplStack - (long *) &Stack_IRQ), xlMemory,
100 * xlMemory / xlRamSize);

// Total heap, SVC stack, and free heap + RAM

for (xplStack = (long *) &Stack_SVC;
xplStack < (long *) &StackTop_SVC && *xplStack == RAM_TEST;
printf (" TOTAL: %5d | SVC: %5d | Heap+RAM: %6d (%d%%)\r\n",
xlHeapTotal, 4 * (xplStack - (long *) &Stack_SVC),
xlMemory + xlHeapFree, 100 * (xlMemory + xlHeapFree) / xlRamSize);

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists