Where Do Code and Data Live? A Deep Dive into Linux Process Memory Layout
This article explains how a running program's code and variables are organized in Linux memory, covering virtual address mapping, the purpose of each segment (text, rodata, data, bss, stack, heap), and demonstrates the concepts with concrete C examples and memory‑map screenshots.
Program Runtime Memory Space Overview
When a program runs, the operating system maps its virtual addresses to physical memory pages. The virtual address space is divided into distinct segments, each holding a specific type of data, and code and data are stored separately.
Memory Segments in a Running Program
Below is a simple C program (named space.c) that prints its own PID and then enters an infinite loop so that the process stays alive. The /proc/$(pid)/maps file shows the memory layout of this process.
#include <unistd.h>
#include <stdio.h>
int main() {
printf("%d
", getpid());
while (1);
return 0;
}The resulting memory map (see the first screenshot) reveals which libraries and regions are loaded. The first column shows the start and end addresses of each segment, the second column the permissions, the third column the offset, and the sixth column the backing file.
Permission flags: r = readable, w = writable, x = executable, p = private (not shared), s = shared.
bss Segment
The bss segment stores uninitialized global variables or globals explicitly initialized to zero. These variables occupy memory at runtime but do not increase the size of the executable file. Example ( bss.c):
#include <stdio.h>
int bss_data[1024*1024];
int main() {
return 0;
}Compiling this program produces an executable of only a few kilobytes, while the bss region in memory spans 4 MiB (shown as rw‑p in the map).
data Segment
The data segment holds initialized global variables with non‑zero values. Unlike bss, the contents of the data segment are stored in the executable file. Example ( data.c):
#include <stdio.h>
int data_data[1024*1024] = {1};
int main() {
return 0;
}The resulting executable is larger (about 4 MiB) because the initialized array is part of the file. In the memory map the data region appears as rw‑p, sharing the same address range as bss but occupying file space.
rodata Segment
The rodata segment stores read‑only constant data (e.g., string literals). Not all constants reside here; some immediate values are embedded directly in the code segment. The segment is marked r‑p in the map, indicating it is protected and cannot be written.
text Segment
The text segment (code segment) contains the executable instructions of the program and some constant integers. It is marked r‑xp —readable and executable but not writable.
stack Segment
The stack holds temporary variables, function parameters, and return addresses. It grows downward (toward lower addresses). Variables declared inside functions (e.g., int a = 0;) reside on the stack, while memory obtained via malloc or new lives on the heap.
void func() {
int a = 0;
int *n_ptr = malloc(sizeof(int));
char *c_ptr = new char;
}When func returns, a is released, but the memory pointed to by n_ptr and c_ptr remains allocated until explicitly freed.
heap Segment
The heap is a large, dynamically managed region where the program can request and release memory at runtime. In C/C++ it is used via malloc, free, new, and delete. Example ( heap.c):
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
int *n_ptr = malloc(sizeof(int));
printf("%d
", getpid());
while (1);
free(n_ptr);
return 0;
}The memory map for this program shows a distinct heap region. Although only a few bytes are requested, the operating system allocates memory in whole pages, so the heap region occupies at least one page.
Anonymous mappings (regions without a file name) in the maps file represent memory obtained via mmap or the heap.
These screenshots illustrate the layout of the various segments (text, rodata, data, bss, stack, heap) for the example programs.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
