37

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.

Collin
  • 1,777
  • 4
  • 26
  • 42

4 Answers4

49

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!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 2
    That helps a lot. But what about fragmentation? So certain things (an array for example) needs to be allocated contiguously. If an array is allocated contiguously in virtual memory, does that mean it's also contiguous in physical memory? Or does that level of fragmentation not occur – Collin Oct 13 '13 at 20:15
  • 5
    @user2079802- Not necessarily. If the array spans several pages of virtual memory, the OS can allocate those pages to completely different regions of physical memory. It's up to the OS to make sure that this doesn't lead to performance issues. – templatetypedef Oct 13 '13 at 20:15
  • 1
    One other thing. When you get the address of some variable, for instance using & in C, that is its virtual address, right? – Collin Oct 13 '13 at 20:23
  • 5
    @user2079802- Yes, that's right. All programs run with the expectation that they control all of memory and all their pointers are based on this assumption. In reality, that memory is scattered all over the place. – templatetypedef Oct 13 '13 at 20:26
  • 1
    I totally agree with the first and last points. The other three are true when you compare having virtual memory with having no memory protection. I would just like to point out that there are CPUs like the Cortex-R series which have a small memory protection unit (MPU). These CPUs don't use virtual addresses, however the MPU prevents one core from reading or writing to places it's not allowed to. A malware wouldn't be able to read memory of another process if the OS configures the MPU correctly. – adentinger Jun 21 '18 at 22:48
12

Short answer: Program code and data required for execution of a process must reside in main memory to be executed, but main memory may not be large enough to accommodate the needs of an entire process.

Two proposals

(1) Using a very large main memory to alleviate any need for storage allocation: it's not feasible due to very high cost.

(2) Virtual memory: It allows processes that may not be entirely in the memory to execute by means of automatic storage allocation upon request. The term virtual memory refers to the abstraction of separating LOGICAL memory--memory as seen by the process--from PHYSICAL memory--memory as seen by the processor. Because of this separation, the programmer needs to be aware of only the logical memory space while the operating system maintains two or more levels of physical memory space.

More:

Early computer programmers divided programs into sections that were transferred into main memory for a period of processing time. As higher level languages became popular, the efficiency of complex programs suffered from poor overlay systems. The problem of storage allocation became more complex.

Two theories for solving the problem of inefficient memory management emerged -- static and dynamic allocation. Static allocation assumes that the availability of memory resources and the memory reference string of a program can be predicted. Dynamic allocation relies on memory usage increasing and decreasing with actual program needs, not on predicting memory needs.

Program objectives and machine advancements in the '60s made the predictions required for static allocation difficult, if not impossible. Therefore, the dynamic allocation solution was generally accepted, but opinions about implementation were still divided.

One group believed the programmer should continue to be responsible for storage allocation, which would be accomplished by system calls to allocate or deallocate memory. The second group supported automatic storage allocation performed by the operating system, because of increasing complexity of storage allocation and emerging importance of multiprogramming.

In 1961, two groups proposed a one-level memory store. One proposal called for a very large main memory to alleviate any need for storage allocation. This solution was not possible due to very high cost. The second proposal is known as virtual memory.

cne/modules/vm/green/defn.html

eeoohee
  • 121
  • 1
  • 5
0

To execute a process its data is needed in the main memory (RAM). This might not be possible if the process is large.

Virtual memory provides an idealized abstraction of the physical memory which creates the illusion of a larger virtual memory than the physical memory.

Virtual memory combines active RAM and inactive memory on disk to form a large range of virtual contiguous addresses. implementations usually require hardware support, typically in the form of a memory management unit built into the CPU.

olle
  • 134
  • 3
  • 15
-2

The main purpose of virtual memory is multi-tasking and running large programmes. It would be great to use physical memory, because it would be a lot faster, but RAM memory is a lot more expensive than ROM.

Good luck!

gpanich
  • 39
  • 1