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

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

问题描述

所以我的理解是每个进程都有自己的虚拟内存空间,范围从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.

如果您尝试通过切换到第二个进程时调出一个进程的所有内存来解决上述两个问题,则会导致 mass 的性能下降,因为您可能必须分页耗尽所有内存.

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天全站免登陆