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
3
u/pjc50 May 15 '25
Virtual memory doesn't have to map to physical memory until the exact moment you're using it.
The MMU can jump in and hand control to the OS, which can then find a page of memory from somewhere. It can also unmap pages which aren't being used. So the OS can take the contents of a physical page, write it to disk, allocate that physical page for a different program to use. Some time later the original program wants its page back: find a new page, read back the memory from disk, and the program doesn't know the difference.