Sharing Knowledge on Embedded Memory Management

Any program requires memory allocation to store the resource information of the process, and C programs are no exception. The areas where variables, constants, functions, and code are stored in a C program differ, and each area has its own characteristics. Learners of C, especially those studying embedded systems, must thoroughly understand these concepts!

The Deceived C Process

When a C program is executed, the system allocates a virtual memory space for each process to facilitate developers’ operations. This virtual memory is actually mapped from physical memory. The starting and ending addresses of virtual memory are fixed, so the layout of virtual memory is the same. For example, if there are three processes P1, P2, and P3, although they receive completely different physical memory, from the perspective of the processes, they all see the same memory.

Sharing Knowledge on Embedded Memory Management

Suppose your computer has only 1GB of actual physical memory, and three processes are currently running on the system. Linux will map some memory in PM to three virtual memory spaces, each 4GB in size, making each process believe it has its own complete memory space. This greatly facilitates the organization of data and code in application layer programs.

Virtual Memory Layout:

The virtual memory layout is divided into kernel space, stack, heap, data segment, code segment, and an inaccessible space (equivalent to a wall).

Sharing Knowledge on Embedded Memory Management

The memory area accessible to a user process ranges from 0x08048000 to 0xc0000000. This “vast” area is further divided into several parts, used to store the process’s code and data. Next, let us further explore what types of data are stored in each space of virtual memory.

Stack Memory

Stack memory is used to store environment variables, command line parameters, and local variables. The stack memory space is very limited, with a default size of 8MB. In embedded development, we should minimize the use of stack space. The stack grows downwards (from high address to low address); whenever a function is called, a segment is allocated from the top down, and this segment is called a stack frame, which is used to store the local variables of that function.

Sharing Knowledge on Embedded Memory Management

When a function exits (call ends), the stack space releases a stack frame from the bottom up, returning all memory to the system.Note: The data values stored in the stack memory are unknown, so it is best to initialize each local variable before use. The stack memory space cannot be manually allocated or released; it is automatically managed by the system, and we cannot intervene.

Heap Space

Heap space is relatively free space, and it is a very important area because we can control the lifecycle of the memory defined in this area: from malloc()/calloc()/realloc() to free(), its allocation and release are entirely defined by us developers, giving us maximum freedom and flexibility to use memory efficiently during program execution.

Sharing Knowledge on Embedded Memory Management

Note:

  • Compared to stack space, heap memory space is much larger.
  • Heap space grows upwards (from low address to high address).
  • Memory in heap space belongs to anonymous space, so pointers are needed to access it.
  • Developers can allocate and release memory themselves; if not released, this space will persist until the program ends.

Data Segment

The data segment stores global variables, static variables, and constants, with a lifecycle that matches the program. As long as the program runs, the data continues (segment).

Sharing Knowledge on Embedded Memory Management

Code Segment

The code segment is further divided into two spaces: .text segment: stores user code (main func …) init segment: performs some initialization tasks at the start of the program (added by the compiler according to the system).

Sharing Knowledge on Embedded Memory Management

Memory management is a key knowledge area in embedded learning and an important indicator of whether someone has entered the field. A good understanding of memory management will deepen your understanding of C language.Related Articles:Memory Leak Issues in C LanguageFragmentation Issues When Allocating Memory with malloc in C LanguageImplementing Simple Dynamic Memory Allocation in C Language

Leave a Comment