内存保留和提交
我正在阅读< Windows 通过 C/C++ >这是一些引述。
当一个进程被创建并给出时 它的地址空间,其中大部分 可用地址空间是空闲的,或者 未分配。要使用此部分 地址空间,您必须分配 通过调用其中的区域 虚拟分配。 分配的行为 区域称为保留。
使用保留的地址区域 空间,您必须分配物理空间 存储,然后将该存储映射到 保留区域。 这个过程是 称为提交物理存储。
您预留区域后,您 需要将物理存储提交给 区域,然后您才能访问 其中包含的内存地址。 系统分配物理存储 致力于一个地区从 系统的分页文件。
这里有几个问题:
为什么我们在使用内存时需要遵循reserve-comit范例?即为什么我们需要遵循这个两步范式而不是直接分配一些物理内存并使用它?
如果提交给某个区域的物理存储是从系统的分页文件中分配的,为什么我们需要 RAM(听起来很荒谬)?在我看来,地址空间区域应该映射到 RAM(通过分页机制),并且 RAM 页面应该由分页文件支持。
也许这个问题可以通过解释以下两个方面来回答:
保留有什么作用?
提交有什么作用?
更新 - 1 2:48 PM 11/23/2010
这是以下引用自< Windows 通过 C/C++ 第五版 >这让我很困惑。
...最好考虑物理因素 存储为分页中存储的数据 磁盘驱动器上的文件。所以当 应用程序提交物理存储 到地址空间区域 调用 VirtualAlloc 函数, 空间实际上是从 硬盘上的文件。
您预留区域后,您 需要将物理存储提交给 区域,然后您才能访问 其中包含的内存地址。 系统从以下位置分配提交给某个区域的物理存储: 系统的分页文件。
那么,RAM 在哪里?如果我将我的机器配置为没有页面文件怎么办?
I am reading < Windows via C/C++ > and here's some quotation.
When a process is created and given
its address space, the bulk of this
usable address space is free, or
unallocated. To use portions of this
address space, you must allocate
regions within it by calling
VirtualAlloc. The act of allocating a
region is called reserving.To use a reserved region of address
space, you must allocate physical
storage and then map this storage to
the reserved region. This process is
called committing physical storage.After you have reserved a region, you
need to commit physical storage to the
region before you can access the
memory addresses contained within it.
The system allocates physical storage
committed to a region from the
system's paging file.
Here are a couple of questions:
Why do we need to follow the reserve-comit paradigm when using memory? i.e. Why do we need to follow this 2-step paradigm instead of allocating some physical memory directly and use it?
If the the physical storage committed to a region is allocated from the system's paging file, why do we need the RAM (sounds ridiculous)? In my opinion, an address space region should be mapped to RAM (through the paging mechanism), and the RAM pages should be backed by the paging file.
Maybe this question could be answered by explaing the following 2 aspects:
What does reserving do?
What does committing do?
Update - 1 2:48 PM 11/23/2010
It is the following quotation from < Windows via C/C++ 5th edition > that makes me puzzled.
...It is best to think of physical
storage as data stored in a paging
file on a disk drive. So when an
application commits physical storage
to a region of address space by
calling the VirtualAlloc function,
space is actually allocated from a
file on the hard disk.After you have reserved a region, you
need to commit physical storage to the
region before you can access the
memory addresses contained within it.
The system allocates physical storage committed to a region from the
system's paging file.
So, where is the RAM? What if I configure my machine to have no page file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保留页面的全部目的是确保连续的地址空间可用于某些任务。例如,我们希望堆栈能够增长到 1MB,但我们不想提交所有内存,因为它实际上还没有被使用。因此,我们保留 1MB 的页面,但提交少量,例如 64kB。通过在提交区域的末尾设置一个保护页,我们可以检测何时需要提交更多内存。
提交内存是将某种存储映射到页面的行为。它可以位于物理 RAM 中(它是工作集的一部分),也可以位于页面文件中。它也可以映射到私有内存中。 NtAllocateVirtualMemory/VirtualAlloc可以同时保留和提交,以方便起见。
编辑更新的问题:当您提交页面时,将根据进程页面文件配额/系统范围的承诺限制进行收费。此限制由可用物理 RAM 量和页面文件大小决定。这实际上并不意味着页面存储在或写入页面文件中。如果内存不足,则可能会出现这种情况,但除此之外,页面大部分都保存在物理内存中。
The whole point of reserving pages is to ensure that contiguous address space is available for some task. For example, we want the stack to able to grow to 1MB, but we don't want to commit all of that memory because it won't actually be used yet. So we reserve 1MB of pages, but commit a small amount, like 64kB. By setting up a guard page at the end of the committed region, we can detect when we need to commit more memory.
Committing memory is the act of mapping some kind of storage to a page. This can be located in physical RAM, where it is part of the working set, or in the pagefile. It can also be mapped in or private memory. NtAllocateVirtualMemory/VirtualAlloc can reserve and commit at the same time, for convenience.
EDIT for updated question: When you commit pages, that is charged against the process pagefile quota / system-wide commitment limit. This limit is determined by the amount of physical RAM available and the size of the pagefile. This doesn't actually mean the pages are stored in, or written to the pagefile. They may be if memory is low, but otherwise pages are mostly kept in physical memory.
您实际上不必遵循两阶段保留/提交分配方案。
重点是,
VirtualAlloc
和VirtualFree
可以做几件事。有时这样做确实很有用。但您不必这样做。提交内存区域是系统为其分配物理存储的区域。
您不必担心它的具体分配位置:RAM 或页面文件。这对您来说应该是透明的(除非您正在编写内核模式设备驱动程序)。而且,大部分内存页面可以交换到页面文件并按需加载到RAM中。
这只是一些提交的内存页面不需要与页面文件绑定,因为它们已经绑定到另一个物理存储。内存映射文件就是一个例子。
在正常情况下,当您提交内存页面时,使用它一段时间并释放它 - 很可能它根本不会到达页面文件。
You don't actually have to follow 2-stage reserve/commit allocation scheme.
The point is that
VirtualAlloc
andVirtualFree
may do several things. And sometimes it's really useful to do so. You don't have to however.Committed memory region is the one for which the system allocates physical storage.
You don't have to worry about where exactly it's allocated: RAM or page file. This should be transparent to you (unless you're writing a kernel-mode device driver). Moreover, most of the memory pages can be swapped out to the page file and loaded into RAM on-demand.
It's just some committed memory pages don't require binding with the page file, because they're already bound to another physical storage. Memory-mapped files are the example of this.
In normal scenario when you commit a memory pages, use it for some time period and free it - most likely it won't reach the page file at all.