虚拟连续内存与物理连续内存 [英] Virtually contiguous vs. physically contiguous memory

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

问题描述

实际上连续的内存在物理上也总是连续的吗?如果没有,如何在物理上不连续的RAM块上分配虚拟的连续内存并进行内存映射?感谢您提供详细的答案.

Is virtually contiguous memory also always physically contiguous? If not, how is virtually continuous memory allocated and memory-mapped over physically non-contiguous RAM blocks? A detailed answer is appreciated.

推荐答案

简短答案:您无需关心(除非您是内核/驱动程序开发人员).一切都一样.

Short answer: You need not care (unless you're a kernel/driver developer). It is all the same to you.

更长的答案:相反,实际上连续的内存在物理上是连续的(通常不是)(仅少量).除非巧合,否则机器刚启动后不久.但这不是必需的.

Longer answer: On the contrary, virtually contiguous memory is usually not physically contiguous (only in very small amounts). Except by coincidence, or shortly after the machine has just booted. That isn't necessary, however.

分配大量物理上连续的RAM的唯一方法是使用大页(因为一页内的内存需要是连续的).但是,这是没有用的,因为您的进程之间没有明显的差异,因为您认为连续的内存是否实际上是连续的,但是使用大页面存在弊端.

The only way of allocating larger amounts of physically contiguous RAM is by using large pages (since the memory within one page needs to be contiguous). It is however a useless endeavor, since there is no observable difference for your process whether memory of which you think that it is contiguous is actually contiguous, but there are disadvantages to using large pages.

物理上不连续的RAM上的内存映射没有特别的特殊"方式.它遵循与所有内存管理相同的方法.

Memory mapping over phyically non-contiuous RAM works in no particularly "special" way. It follows the same method which all memory management follows.

操作系统将虚拟内存划分为页面",并为您的进程创建页面表条目.当您访问某个位置的内存时,相应的页面根本不存在,或者它存在并且与RAM中的 real 页面相对应,或者它存在但不存在对应于RAM中的 real 页面.

The OS divides virtual memory in "pages" and creates page table entries for your process. When you access a memory in some location, either the corresponding page does not exist at all, or it exists and corresponds to a real page in RAM, or it exists but doesn't correspond to a real page in RAM.

如果页面存在于RAM中,则完全没有任何反应 1 .否则,将生成故障并运行某些操作系统代码.如果事实证明该页面根本不存在(或没有正确的访问权限),则您的进程将因分段错误而终止.

If the page exists in RAM, nothing happens at all1. Otherwise a fault is generated and some operating system code is run. If it turns out the page doesn't exist at all (or does not have the correct access rights), your process is killed with a segmentation fault.

否则,操作系统会选择一个不使用的任意页面(或者交换掉它认为最不重要的页面),然后将数据从磁盘加载到该页面中.对于内存映射,数据来自映射文件,否则来自交换(对于全新分配的内存,将复制零页).然后,操作系统将控制权返回给您的进程.你永远都不知道这件事发生了.

Otherwise, the OS chooses an arbitrary page that isn't used (or it swaps out the one it thinks is the least important one), and loads the data from disk into that page. In the case of a memory mapping, the data comes from the mapped file, otherwise it comes from swap (and for completely new allocated memory, the zero page is copied). The OS then returns control back to your process. You never know this happened.

如果您访问位于不同页面的连续"(或您认为!)存储区中的另一个位置,则将执行完全相同的过程.

If you access another location in a "contiguous" (or so you think!) memory area which lies in a different page, the exact same procedure runs.


1 实际上,这有点复杂,因为页面可能存在于RAM中,但并不正式地"存在,而是要回收的页面列表的一部分.但这太复杂了.


1 In reality, it is a little more complicated, since a page may exist in RAM but not exist "officially", being part of a list of pages that are to be recycled or such. But this gets too complicated.

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

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