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

61 comments sorted by

View all comments

Show parent comment

1

u/[deleted] May 15 '25 edited May 15 '25

hmm in that case wouldn't the OS not be able to give the memory asked for by process 3 ?

I could be wrong, but when a process asks for memory, it is always a contiguous block ?

1

u/apnorton May 15 '25 edited May 15 '25

I could be wrong, but when a process asks for memory, it is always a contiguous block ?

It isn't, no. (At least, not from the perspective of the physical memory.) There's a bit of "blocky-ness" from pages, but virtual pages do not have to be next to each other on physical memory. This is also key to how dynamic memory allocation works. (e.g. what if you have process 1 with 1gb memory, process 2 with 0.5gb memory immediately after it, and then process 1 wants to increase its allocation by 0.5gb?)

1

u/[deleted] May 15 '25

so a process could get contiguous virtual memory block, but physically it can be all over the place ?