Does malloc Allocate Memory? Exploring Virtual Memory, brk, and Page Faults
This article explains why calling malloc in C only reserves virtual address space, how the kernel maps it to physical memory on demand, and the roles of the brk pointer, memory layout, and page‑fault handling, illustrated with a 1 GB allocation example and detailed diagrams.
Memory Allocation in C
In languages with automatic garbage collection such as Go, Java, or Python, developers rarely manage memory directly, but C/C++ programmers must explicitly request memory using malloc. The article starts with a simple program that calls malloc(1024*1024*1024) to request 1 GB and then sleeps for an hour, allowing observation of its memory usage.
#include <stdlib.h>
int main() {
void *ptr;
ptr = malloc(1024 * 1024 * 1024); // request 1 GB
sleep(3600); // keep process alive
return 0;
}When compiled and run, tools show that the process’s VmRSS (resident set size) is only about 404 KB, far less than the requested 1 GB. This discrepancy leads to a discussion of virtual memory versus physical memory.
Physical Memory vs. Virtual Memory
Physical memory refers to the actual RAM modules installed in a computer (e.g., a 2 GB DIMM occupies addresses 0 ~ 2 GB). Virtual memory is an abstraction that provides each process with its own address space, typically 0 ~ 4 GB on a 32‑bit OS. The CPU can only access physical addresses, so the Memory Management Unit (MMU) translates virtual addresses to physical ones on demand.
The kernel divides a process’s virtual address space into several regions:
Code segment : executable instructions.
Data segment : global and static variables.
Heap : memory obtained via malloc (or mmap).
Stack : function parameters and local variables.
Kernel space : Linux kernel code and data.
The brk Pointer
The heap grows upward from a pointer called brk. Moving brk upward expands the heap, while moving it downward releases memory. The Linux system call brk() adjusts this pointer, and the standard malloc implementation uses it to satisfy allocation requests (ignoring the mmap path for simplicity).
For example, allocating 1024 bytes simply moves brk up by 1024 bytes, as shown in the accompanying diagram.
Memory Mapping and Page Faults
Although malloc reserves virtual addresses, no physical pages are allocated until the program accesses them. If a process reads or writes an unmapped virtual address, the CPU raises a page‑fault exception. The kernel then:
Identifies the faulting virtual address.
Checks whether the address lies within the current brk range; if not, the process receives a segmentation fault.
If the address is within the brk range, the kernel allocates a physical page, updates the page tables, and resumes execution.
This lazy allocation explains why a 1 GB malloc call may only consume a few hundred kilobytes of physical RAM until the program actually touches the memory.
Conclusion
The article clarifies that malloc reserves virtual memory, while physical memory is allocated on demand during page‑fault handling. This design reduces the immediate pressure on RAM and allows the operating system to manage memory more efficiently.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
