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




Static Vs Dynamic Memory Allocation

Introduction

FreeRTOS versions prior to V9.0.0 allocate the memory used by the RTOS objects listed below from the special FreeRTOS heap. FreeRTOS V9.0.0 and onwards gives the application writer the ability to instead provide the memory themselves, allowing the following objects to optionally be created without any memory being allocated dynamically:
  • Tasks
  • Software Timers
  • Queues
  • Event Groups
  • Binary Semaphores
  • Counting Semaphores
  • Recursive Semaphores
  • Mutexes
Whether it is preferable to use static or dynamic memory allocation is dependent on the application, and the preference of the application writer. Both methods have pros and cons, and both methods can be used within the same RTOS application.

The simple Win32 example located in the FreeRTOS/Source/WIN32-MSVC-Static-Allocation-Only directory of the main FreeRTOS download demonstrates how a FreeRTOS application can be created without including any of the FreeRTOS heap implementations in a project.


Creating an RTOS Object Using Dynamically Allocated RAM

Creating RTOS objects dynamically has the benefit of greater simplicity, and the potential to minimise the application's maximum RAM usage:
  • Fewer function parameters are required when an object is created.

  • The memory allocation occurs automatically, within the RTOS API functions.

  • The application writer does not need to concern themselves with allocating memory themselves.

  • The RAM used by an RTOS object can be re-used if the object is deleted, potentially reducing the application's maximum RAM footprint.

  • RTOS API functions are provided to return information on heap usage, allowing the heap size to be optimised.

  • The memory allocation scheme used can be chosen to best suite the application, be that heap_1.c for simplicity and determinism often necessary for safety critical applications, heap_4.c for fragmentation protection, heap_5.c to split the heap across multiple RAM regions, or an allocation scheme provided by the application writer themselves.

The following API functions, which are available if configSUPPORT_DYNAMIC_ALLOCATION is set to 1 or left undefined, create RTOS objects using dynamically allocated RAM:


Creating an RTOS Object Using Statically Allocated RAM

Creating RTOS objects using statically allocated RAM has the benefit of providing the application writer with more control:
  • RTOS objects can be placed at specific memory locations.

  • The maximum RAM footprint can be determined at link time, rather than run time.

  • The application writer does not need to concern themselves with graceful handling of memory allocation failures.

  • It allows the RTOS to be used in applications that simply don't allow any dynamic memory allocation (although FreeRTOS includes allocation schemes that can overcome most objections).

The following API functions, which are available if configSUPPORT_STATIC_ALLOCATION is set to 1, allow RTOS objects to be created using memory provided by the application writer. To provide memory the application writer simply needs to declare a variable of the appropriate object type, then pass the address of the variable into the RTOS API function. The StaticAllocation.c standard demo/test task is provided to demonstrate how the functions are used:







[ 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