Although the question specifically mentions embedded systems, the title only mentions proc/<pid>/maps
, which is also very useful for understanding "normal" programs. In this broader context, it's important to realize that memory allocated by malloc()
can end up either in the heap or in any number of anonymous memory segments. Big blocks of anonymous memory, therefore, are likely to have come from malloc()
.
What /proc/<pid>/maps
refers to as [heap]
is more precisely a contiguous region between the memory allocated for static variables (called the BSS segment) and an address called the "program break" (see diagram below). Initially, this region is empty and there is no heap. When malloc()
is called, it can create/expand the heap by asking the kernel—via the brk()
syscall—to move the program break. Likewise, free()
can shrink the heap if all the addresses adjacent to the program break are no longer in use.
However, moving the program break is not the only way that malloc()
can make more room for itself. It can also ask the kernel—via the mmap()
syscall—to reserve a block of space somewhere between the stack and the heap (see diagram below). Memory allocated in this way appears in /proc/<pid>/maps
as the "anonymous inode 0 entries" mentioned in the question.
Image credit
It's worth elaborating on the mmap()
syscall a bit. There are four kinds of memory maps that mmap()
can create, and they are each used for very different purposes. First, the memory can either be tied to the contents of a certain file, or not. The latter is called an "anonymous" map. Second, the memory can either be "private" or "shared". Private means that changes made by one process will not be visible to any others; this is usually implemented in a lazy and efficient manner called "copy-on-write". Shared means that each process will get access to the same underlying physical memory. Below are the uses that I am aware of for each kind of memory map:
Going back to /proc/<pid>/maps
, you can figure out which kind of memory map each line describes by looking at the "pathname" and "perms" columns. (These column names come from the kernel docs). For file maps, the "pathname" column will hold an actual path to the file being mapped. For anonymous maps, the "pathname" column will be empty. There are also some special path names like [heap]
and [stack]
. For private and shared maps, the "perms" column will include the p
or s
flag, respectively.
Current implementations of malloc()
use brk()
for small allocations and mmap()
for large ones. It makes sense to allocate small amounts of memory on the heap, because it is very often possible to find the necessary space without having to make an expensive syscall (e.g. by reusing previously freed space). However, large allocations run the risk of never being released back to the operating system. Consider what would happen if you were to make a big allocation on the heap followed by a bunch of small ones. Even if after the big allocation is freed, the program break couldn't be moved back until all the small allocations were also freed. This simple example assumes that the allocations go onto the heap in order, which is a naive approach, but it illustrates how the heap makes it much harder to free memory back to the operating system.
Here's the relevant section from man malloc
:
Normally, malloc() allocates memory from the heap, and adjusts the size of the heap as required, using sbrk(2). When allocating blocks of memory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation allocates the memory as a private anonymous mapping using mmap(2). MMAP_THRESHOLD is 128 kB by default, but is adjustable using mallopt(3). Prior to Linux 4.7 allocations performed using mmap(2) were unaffected by the RLIMIT_DATA resource limit; since Linux 4.7, this limit is also enforced for allocations performed using mmap(2).
In summary, if your program uses malloc()
, then malloc()
is likely responsible for many of the large, anonymous segments that get mapped into virtual memory and reported by /proc/<pid>/maps
.
Caveat emptor: Pretty much everything I wrote here I just learned today, so take it with a grain of salt. That said, here are links to resources I found very helpful for understanding all of this: