Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need virtual memory?

So my understanding is that every process has its own virtual memory space ranging from 0x0 to 0xFF....F. These virtual addresses correspond to addresses in physical memory (RAM). Why is this level of abstraction helpful? Why not just use the direct addresses?

I understand why paging is beneficial, but not virtual memory.

like image 721
Collin Avatar asked Oct 13 '13 19:10

Collin


People also ask

Why is virtual memory needed?

Virtual memory frees up RAM by swapping data that has not been used recently over to a storage device, such as a hard drive or solid-state drive (SSD). Virtual memory is important for improving system performance, multitasking and using large programs.

Why do we need virtual memory answer it with 4 reasons?

The main advantage of virtual memory is that an OS can load programs larger than its physical memory. It makes an impression to the users that the computer has unlimited memory. It also provides memory protection. In order to realize the mapping operations, virtual memory needs to use page tables and translations.

What would happen without virtual memory?

If there were no such thing as virtual memory, then once you filled up the available RAM your computer would have to say, "Sorry, you can not load any more applications.

Do I need virtual memory if I have enough RAM?

Therefore, no matter how large the capacity of RAM is, it's still necessary for us to enable the virtual memory. Another thing about virtual memory is that Windows only uses paging files when it's necessary. In other words, Windows does not use paging files all the time.


1 Answers

There are many reasons to do this:

  • If you have a compiled binary, each function has a fixed address in memory and the assembly instructions to call functions have that address hardcoded. If virtual memory didn't exist, two programs couldn't be loaded into memory and run at the same time, because they'd potentially need to have different functions at the same physical address.

  • If two or more programs are running at the same time (or are being context-switched between) and use direct addresses, a memory error in one program (for example, reading a bad pointer) could destroy memory being used by the other process, taking down multiple programs due to a single crash.

  • On a similar note, there's a security issue where a process could read sensitive data in another program by guessing what physical address it would be located at and just reading it directly.

  • If you try to combat the two above issues by paging out all the memory for one process when switching to a second process, you incur a massive performance hit because you might have to page out all of memory.

  • Depending on the hardware, some memory addresses might be reserved for physical devices (for example, video RAM, external devices, etc.) If programs are compiled without knowing that those addresses are significant, they might physically break plugged-in devices by reading and writing to their memory. Worse, if that memory is read-only or write-only, the program might write bits to an address expecting them to stay there and then read back different values.

Hope this helps!

like image 91
templatetypedef Avatar answered Oct 21 '22 23:10

templatetypedef