r/AskComputerScience • • May 14 '25
confused about virtual memory
If I got this right, the point of virtual memory is to ensure processes use unique physical address space.
Is this abstraction really needed ?
For example, say there are 2 C programs and each one does malloc. This asks the OS for memory. Why can't the OS guarantee that unique physical address space is given to the C program ?
2
Upvotes
View all comments
Show parent comment
2
u/dkopgerpgdolfg May 15 '25
In addition to apnorton:
Programs might also over-allocate meory, ie. ask the OS for memory that they won't use then later. (various reasons for that, not really important here).
In a simple system, this means that a valueable part of physical memory goes unused, and you can run less things at the same time because misbehaving programs.
However, in practice, MMU mappings between virtual and physical memory have some nice features that help: You can eg. give a virtual address range to a program that doesn't go anywhere, not backed by any memory. Only when the program accesses this address range for the first time, the OS is notified and can create a real physical memory mapping. => Meaning, the program can believe it has 10000GB memory while the physical RAM only holds 16GB, as long as it doesn't actually write data in all 10000GB.