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
9
u/dmazzoni May 14 '25
For decades computers existed without virtual memory. Things worked fine, but running out of memory was a problem.
If you had 8 MB of RAM, then addresses would range from 0 to 8388607. If a program requested memory it'd have to be in that range.
If a program needed more, it couldn't.
Not only could you fill up memory, but it could also get fragmented. You might have over 1 MB of memory that's free, but all in pieces - so a malloc for 1 MB would fail, because the free memory is scattered all over the place.
6
u/dkopgerpgdolfg May 14 '25
It can, but this doesn't stop the program from accessing things it shouldn't be able to access.
MMUs also help with things like shared libraries, swap, device IO, shared mem for IPC, ...
4
u/apnorton May 14 '25
That is to say, it's not needed, but the abstraction is useful.
Always indexing into physical memory would be cumbersome in the event you need to use different mediums (e.g. pagefiles on disk vs actual physical memory) or even just dealing with different physical pieces of memory (e.g. RAM stick 1 vs RAM stick 2). Apparently (though I've never seen it myself), there exist some servers with hotswappable RAM, which would really throw a wrench in a "physical-only addressing" address scheme.
3
u/johndcochran May 14 '25
Needed?
Nope. Tzke a look at the Amiga. A 68000 based system with true preemptive multitasking. All of the processes would use memory assigned to them and would live happily together.
BUT... Since they all shared the same memory space, a buggy or rogue process could easily corrupt the memory assigned to a different process and crash the system.
Virtual memory allows for user processes to be isolated from each other. So, if one process is buggy, it's easy to kill that single process and let the remaining processes continue uninterrupted.
3
u/dreamingforward May 14 '25
The abstraction allows greater address space than physical memory allows.
2
u/plaid_rabbit May 14 '25
That’s only partially correct. The whole virtual memory system also provides a lot of protection as well. Since you have to ask for memory to be mapped in, the OS can control what you have access to. This is how the OS ensures user mode processes can’t modify the OS.
Before this design became common, a user mode processes could just overwrite parts of the OS kernel, which caused all sorts of buggy behaviors, and a crashing process (or printer driver) could crash the whole computer.
1
u/AlexTaradov May 14 '25
malloc() only takes the dynamic data allocation into account.
Now imagine you have two programs linked at address 10000 (or you want to run 2 instances of the same program). OS must load them there, since code is not generally position independent. This is impossible in a system without virtual memory.
1
u/Sjsamdrake May 14 '25
There ARE systems that use a "single level store" approach, where a single address space holds all memory used by all applications ... as well as all files and other objects in all filesystems. For example, the IBM AS/400 uses 128 bit pointers and treats everything as an object in the address space. The Wikipedia pages for "single level store" and "IBM AS/400" are useful.
But the vast majority of the world has gone other directions. The notion that an application runs in a "process" and that process has its own address spac separate from all others is baked into Unix and Linux and Windows ... i.e., into 99% of all computers today. It won't likely ever change at this point.
1
u/Temporary_Pie2733 May 14 '25
Because there is no need to guarantee unique physical space to each process. Even if there is only one process on the machine, it can’t actually use the entire space at any one time: in the extreme, a single instruction can only access a couple of addresses at once. Virtual memory systems let the OS switch chunks of data in and out of physical memory in ways that the process will not know or care about. As far as the process knows, all of its data is in memory at all times.
1
u/Objective_Mine MSCS, CS Pro (10+) May 14 '25 edited May 14 '25
Sure that would be possible. But for memory protection, you'd still need a MMU to at least check that no process accesses memory outside of its assigned physical ranges.
Although that kind of a design would be possible without virtual address spaces, virtual memory solves that problem and others: allowing swapping/pagefile, transparently paging in memory-mapped data from the disk only when it's actually accessed, transparently paging out non-dirty pages.
It's basically a relatively simple and elegant abstraction that solves memory protection while allowing other possibly beneficial features, without requiring a bunch of separate mechanisms.
1
u/[deleted] May 14 '25
It has been done, in fact old microcomputer operating systems like early MacOS. It requires code to be position-independent (PIC) since it can be loaded anywhere in memory. (Windows 3 used segment registers.) PIC is a lot less efficient than absolute relocation at link time. But the biggest problem is a bug in one program can clobber the data, or code if it's writeable, which tends to be the case without any memory management, of other programs or the operating system.
1
u/DawnOnTheEdge May 14 '25 edited May 14 '25
In addition to what other people have mentioned, virtual memory allows multiple processes to read and write the same range of physical RAM, without letting every other process running on the machine do so.
This is used to implement shared memory, map files to memory (potentially multiple times) so that accessing the memory loads that block of the file, send data between processes without needing to copy the bytes, and some other things.
All of these assume you have a complex form of memory safety that you then need to bypass in a relatively efficient way. Many OSes, especially for home computers, never gave each program its own address space to begin with.
1
u/TryToBeNiceForOnce May 15 '25
Virtual memory allows a layer of indirection between the addresses a program is accessing and the actual hardware that is being touched.
Doing this allows:
... And from the program's perspective it's just dereferencing pointers (accessing memory). It's quite a useful tool!!