为什么需要虚拟内存? [英] Why do we need virtual memory?

查看:34
本文介绍了为什么需要虚拟内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的理解是每个进程都有自己的虚拟内存空间,范围从 0x0 到 0xFF....F.这些虚拟地址对应于物理内存 (RAM) 中的地址.为什么这种抽象级别有帮助?为什么不直接使用直接地址?

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.

推荐答案

这样做的原因有很多:

  • 如果你有一个编译好的二进制文件,每个函数在内存中都有一个固定的地址,调用函数的汇编指令会对该地址进行硬编码.如果虚拟内存不存在,两个程序就不能被加载到内存中并同时运行,因为它们可能需要在同一个物理地址上有不同的功能.

  • 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.

根据硬件,某些内存地址可能会保留给物理设备(例如,视频 RAM、外部设备等).如果在不知道这些地址重要的情况下编译程序,它们可能会物理中断插入-通过读取和写入其内存来输入设备.更糟糕的是,如果该内存是只读或只写的,程序可能会将位写入一个地址,希望它们留在那里,然后读回不同的值.

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.

希望这有帮助!

这篇关于为什么需要虚拟内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆