Why malloc() Allocates Zero Physical RAM: Virtual Memory and Page Faults Explained
When a program calls malloc() to request memory in C or similar languages, the operating system does not immediately assign any physical RAM to the process. Instead, the kernel creates a virtual memory mapping by recording a new address range in the process's memory descriptor, leaving page table entries marked as invalid. Physical memory is only committed when the CPU actually tries to read or write a byte within that range, triggering a hardware event called a page fault. At that point, the CPU traps into the kernel, which walks a multi-level page table structure to locate or assign a real physical memory page. This lazy allocation strategy, managed through the Memory Management Unit and the kernel's virtual memory subsystem, allows operating systems to efficiently overcommit memory across many processes.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in