Understanding Memory Allocation in Embedded Systems

Understanding Memory Allocation in Embedded Systems

Introduction

Memory allocation is a crucial aspect of embedded systems design. Understanding how memory is organized and utilized helps in optimizing performance, ensuring reliability, and reducing resource wastage. This article covers the memory allocation of local, global, and static variables, along with key areas like bootloader, diagnostics, and program memory in an MCU (Microcontroller Unit).


Memory Segmentation in an MCU

Embedded systems typically follow a structured memory map, which includes the following segments:

  1. Code/Program Memory (Flash/ROM) – Stores the firmware code.
  2. Static Data Segment (RAM/Flash) – Stores global and static variables.
  3. Stack (RAM) – Stores local variables and function calls.
  4. Heap (RAM) – Stores dynamically allocated memory.
  5. Bootloader Memory (Flash) – Stores the bootloader code.
  6. Diagnostic Memory (RAM/Flash) – Stores system logs and fault data.

CPU Memory Block Diagram

Below is a typical memory layout for an MCU, showing memory areas along with example addresses and data stored:

+-------------------------+ 0x08000000 (Flash Start)
| Bootloader             |
+-------------------------+
| Program Code           |
+-------------------------+
| Constant Data          |
+-------------------------+ 0x080FFFFF (Flash End)

+-------------------------+ 0x20000000 (RAM Start)
| Global & Static Vars   |
+-------------------------+
| Heap (Dynamic Memory)  |
+-------------------------+
| Stack (Function Calls) |
+-------------------------+ 0x2001FFFF (RAM End)
        

This structure varies based on the MCU architecture but generally follows this format.


1. Local Variables (Stack Memory Allocation)

Local variables are created within a function and exist only during the function execution. They are stored in the stack memory.

Characteristics:

  • Memory is allocated and deallocated automatically when function calls occur.
  • Fast access but limited in size.
  • Cannot be accessed outside the function scope.

Example:

void exampleFunction() {
    int localVar = 10;  // Allocated in stack memory
}
        

2. Global Variables (Data/BSS Segment in RAM/Flash)

Global variables are declared outside any function and are accessible throughout the program. These are stored in the data (initialized) or BSS (uninitialized) segments of memory.

Characteristics:

  • Retain their values throughout program execution.
  • Can be modified by any function, leading to potential data integrity issues.
  • Increase RAM usage if overused.

Example:

int globalVar = 100;  // Stored in the data segment
        

3. Static Variables (Data/BSS Segment in RAM)

Static variables maintain their values between function calls and are stored in the BSS or data segment.

Characteristics:

  • Lifetime extends throughout program execution.
  • Not visible outside the function (if declared within a function).
  • Helps in preserving state information.

Example:

void counterFunction() {
    static int count = 0;  // Stored in data segment, retains value
    count++;
}
        

4. Bootloader Memory (Flash/ROM)

A bootloader is a small piece of software stored in Flash/ROM memory that is responsible for initializing hardware and loading the main application.

Characteristics:

  • Used for firmware updates and system bootstrapping.
  • Occupies a dedicated memory section, usually at the beginning of Flash memory.
  • Runs before the main application starts.

Example:

  • MCU Bootloader Addressing:


5. Diagnostic Memory (RAM/EEPROM/Flash)

Diagnostics in embedded systems involve logging faults, monitoring system health, and debugging issues. Diagnostic memory is allocated in RAM, EEPROM, or Flash depending on the application.

Characteristics:

  • Stores error codes, logs, and fault reports.
  • May require non-volatile storage (EEPROM or Flash) to retain data after power loss.
  • Essential for automotive and industrial embedded applications.

Example Usage:

#define DIAG_MEM_ADDR 0x0800F000  // Flash memory for diagnostics
uint8_t diagnosticData[256];  // Buffer stored in RAM
        

6. Program Memory (Flash/ROM)

The program memory (typically Flash/ROM) holds the firmware code and constants.

Characteristics:

  • Non-volatile memory, retains content after power cycles.
  • Limited number of write cycles (important for firmware updates).
  • Can be re-programmed using a bootloader.

Example Usage:

const char message[] = "Hello, Embedded World!";  // Stored in Flash
        

Memory Allocation Summary Table

Memory Type Storage Location Lifetime Usage Local Variables Stack (RAM) Function Scope Temporary data storage Global Variables Data/BSS Segment (RAM) Entire Program Execution Shared data across functions Static Variables Data/BSS Segment (RAM) Function Scope (Retains Value) State retention Bootloader Flash/ROM Permanent Firmware loading Diagnostic Memory RAM/EEPROM/Flash Depends on implementation System health logging Program Memory Flash/ROM Permanent Stores firmware and constants


Conclusion

Understanding memory allocation in embedded systems is essential for optimizing performance and ensuring efficient resource utilization. Proper use of local, global, and static variables, along with careful management of bootloader, diagnostics, and program memory, helps in designing robust firmware for embedded applications.

🚀 What memory management techniques do you use in your embedded projects? Share your thoughts in the comments!

Vivek Bhageria

Building Smart Embedded & IoT Solutions | Tech Innovator | Cloud & AI Integration

1w

While this info is not wrong, it isn't entirely correct. Many embedded systems run entirely from RAM. The entire program is copied into RAM and executed from the RAM. Examples are systems running linux, say raspberry pi based. There are also non linux based systems that follow this approach.

Like
Reply
Vijay Bari

Technical Lead at KPIT with expertise in Autosar, Python, and C

4w

In our project, memory management is implemented using AUTOSAR NVM and FEE modules.

Like
Reply
Krishnan R

Aspiring AI & Computer Vision Engineer | Beginner in Generative AI | Data Enthusiast

1mo

well explained

Like
Reply
Rajashree Arabhanvi

Embedded software | Firmware developer | IOT developer | Embedded C | Bare metal | MCU

1mo

Interesting

Like
Reply
Ashwani Kesharwani

Firmware / Embedded Software

1mo

well explained

Like
Reply

To view or add a comment, sign in

More articles by Srishti Sharma

Insights from the community

Others also viewed

Explore topics