Memory Management in Embedded Linux

embedded linuxIn any computer system, memory is a scarce resource. No matter how much memory is available,

it never seems to be enough. It doesn’t seem so long ago that 256MB of RAM was considered sufficient, but now 2GB of RAM is a minimum requirement even for desktop systems, with servers having more.

Linux applications, except for a few bare-metal embedded applications, are never permitted to access physical memory directly. It might appear so to the application, but what the application is seeing is a controlled illusion.

Linux provides applications with a huge, directly addressable memory space.

Additionally, it provides protection so that different applications are protected from each other,

and it allows applications to apparently access more memory than is physically present in the

machine, provided the machine is at least well configured and has sufficient swap space.

Simple Memory allocation is done using C malloc standard library function as shown below:

Displaying

The malloc function returns a pointer to the heap section of the memory. It returns a void * pointer, so you cast the result to any data type that you need.

The variable “size” can be controlled so that “malloc” can allocate more memory as per our need. This is done by assigning a #define macro constant to “size.” 

The below example will explain how does kernel does memory management and controls the situation when an application is using more memory than the physical RAM. Let’s assume the physical RAM size is 2GB.

#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#define ONE_K (1024)

int main()

{

char *some_memory;

int size_to_allocate = ONE_K;

int megs_obtained = 0;

int ks_obtained = 0;

while (1) 

{

for (ks_obtained = 0; ks_obtained < 1024; ks_obtained++)

 {

   some_memory = (char *)malloc(size_to_allocate);

   if (some_memory == NULL) exit(EXIT_FAILURE);

   sprintf(some_memory, “Hello World”);

 }

megs_obtained++;

printf(“Now allocated %d Megabytes\n”, megs_obtained);

}

exit(EXIT_SUCCESS);

}

What is your observation?

The above code simply loops, asking for more and more memory. For every 1K of memory allocated, “megs_obtained” is incremented. At one point of time, the program will allocate and access more memory than the physical RAM. This is where the system protects itself from this rather aggressive program and kills it. 

Observation result:

Displaying

How it work?

The application’s allocated memory is managed by the Linux kernel. Each time the program asks for memory or tries to read or write to memory that it has allocated, the Linux kernel takes charge and decides how to handle the request.

Initially, the kernel was simply able to use free physical memory to satisfy the application’s request for memory, but once physical memory was full, it started using what’s called swap space. On Linux, this is a separate disk area allocated when the system was installed. 

Linux implements a demand paged virtual memory system. All memory seen

by user programs is virtual; that is, it doesn’t actually exist at the physical address the program uses. Linux divides all memory into pages, commonly 4,096 bytes per page. When a program tries to access memory, a virtual-to-physical translation is made, although how this is implemented and the time it takes depend on the particular hardware you’re using. When the access is to memory that isn’t physically resident, a page fault results and control is passed to the kernel.

 

Avatar photo

Technical Lead
Susmita is focused on delivering effective training to the learners in front-end RTL design and Design for Testability and she writes articles that help our readers gain good knowledge on such VLSI topics.
Whatsapp