什么是虚拟内存? [英] What is virtual memory?

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

问题描述

什么是虚拟内存,它与物理内存 (RAM) 有何不同?它说物理内存存储在主板上,而虚拟内存存储在磁盘上.

What is virtual memory and, how it differs from physical memory (RAM)? It says that physical memory is stored on sth on motherboard, while virtual memory is stored on disk.

某处还说虚拟空间仅在物理内存已满时使用,这让我很困惑.

Somewhere it also says that virtual spaces are used only when the physical memory is filled, which confused me a lot.

那么,为什么 Windows 使用虚拟内存?是不是因为内存空间小,不是专为大存储而设计的,所以使用虚拟来存储更多更大的东西?

Then, why Windows uses virtual memory? Is it because the RAMs are small-spaced and not designed for big storage, so use the virtual to store more bigger-sized things?

接下来是地址.由于虚拟机位于磁盘上,因此它们不应共享物理机的地址.所以他们有独立的地址.是吗?

The next thing is about the address. Since virtuals are on disk, they shouldn't share the address of physicals. So they have independent addresses. Is that right?

还有,

在写入另一个进程的内存时,为什么推荐使用 VirtualAlloc 而不是 HeapAlloc?

虚拟内存是依赖于进程的,物理内存是通过进程共享的,这是真的吗?

推荐答案

Virtual memory"意味着存在有效的地址空间,它不映射到任何特定的物理内存或存储,因此 virtual.在现代通用操作系统中,每个进程都有自己的虚拟内存空间,虚拟内存地址重叠.

"Virtual memory" means there is a valid address space, which does not map to any particular physical memory or storage, hence virtual. In context of modern common operating systems, each process has its own virtual memory space, with overlapping virtual memory addresses.

这个地址空间被分成页以便于管理(示例大小为 4 KB).每个有效页面可以处于 3 种不同的状态:

This address space is divided into pages for easier management (example size 4 KB). Each valid page can be in 3 different states:

  • 未物理存储(假设全为 0).如果进程写入此类页面,则需要为其分配一个物理内存页面(由操作系统,见下文),以便存储值.
  • 映射到物理内存,意味着计算机 RAM 中的一些页面大小区域存储内容,并且它们可以被进程直接使用.
  • 交换到磁盘(可能是交换文件),以释放物理 RAM 页面(由操作系统自动完成).如果进程访问页面(读取或写入),则需要先将其加载到 RAM 中的页面(见上文).

只有当虚拟内存页面映射到物理内存页面时,才会有东西存在.在其他情况下,如果进程访问该页面,则会出现 CPU 异常,它将控制权转移给操作系统.然后操作系统需要将该虚拟内存页面映射到 RAM(可能需要首先通过将当前数据交换到交换文件来释放一些 RAM,或者如果所有内存不足则终止某些应用程序)并将正确的数据加载到其中,或者它可以终止应用程序(地址不在有效范围内,或为只读但进程尝试写入).

Only when virtual memory page is mapped to physical RAM page, is there something there. In other cases, if process accesses that page, there is a CPU exception, which transfers control to operating system. OS then needs to either map that virtual memory page to RAM (possibly needing to free some RAM first by swapping current data out to swap file, or terminating some application if out of all memory) and load the right data into it, or it can terminate the application (address was not in valid range, or is read-only but process tries to write).

同一页内存也可以同时映射到多个地方,例如共享内存,这样同一数据可以同时被多个进程访问(虚拟地址可能不同,所以不能共享指针变量).

Same page of memory can also be mapped to several places at once, for example with shared memory, so same data can be accessed by several processes at once (virtual address is probably different, so can't share pointer variables).

虚拟内存使用的另一个特殊情况是将磁盘上的常规文件映射到虚拟内存(与交换文件发生的情况相同,但现在由正常应用程序进程控制).然后操作系统负责从磁盘实际读取字节(以页面大小的块)并将更改写回,该进程可以像访问任何内存一样访问内存.

Another special case of virtual memory use is mapping a regular file on disk to virtual memory (same thing which happens with swap file, but now controlled by normal application process). Then OS takes care of actually reading bytes (in page-sized chunks) from disk and writing changes back, the process can just access the memory like any memory.

每个现代多任务通用操作​​系统都使用虚拟内存,因为它们运行的​​ CPU 支持它,并且因为它解决了一大堆问题,例如内存碎片、透明地使用交换到磁盘、内存保护......它们可以以不同的方式解决,但虚拟内存是今天的方式.

Every modern multi-tasking general purpose operating system uses virtual memory, because the CPUs they run support it, and because it solves a big bunch of problems, for example memory fragmentation, transparently using swapping to disk, memory protection... They could be solved differently, but virtual memory is the way today.

进程间共享物理内存的方式与共享计算机电源或共享 CPU 的方式相同.它是物理计算机的一部分.一个正常的进程从不处理实际的物理内存地址,它看到的只是虚拟内存,它可能被映射到不同的物理位置.

Physical memory is shared between processes the same way as computer power supply is shared, or CPU is shared. It is part of the physical computer. A normal process never handles actual physical memory addresses, all that it sees is virtual memory, which may be mapped to different physical locations.

虚拟内存的内容通常不被共享,除非它们是(例如使用共享内存时).

The contents of virtual memory are not normally shared, except when they are (when using shared memory for example).

不确定您的意思是为其他进程收集内存时",所以无法回答.

Not sure you mean by "When collecting memory for other process", so can't answer that.

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

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