关于Windows:内存保留和提交

您所在的位置:网站首页 内存保留区 关于Windows:内存保留和提交

关于Windows:内存保留和提交

2024-07-09 12:46| 来源: 网络整理| 查看: 265

我正在阅读,这是一些引用。

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.

这里有几个问题:

为什么在使用内存时我们需要遵循保留提交范式?即为什么我们需要遵循此两步模式,而不是直接分配一些物理内存并使用它?

如果从系统的页面文件分配了分配给区域的物理存储,为什么我们需要RAM(听起来很可笑)?我认为,应该将地址空间区域映射到RAM(通过分页机制),并且RAM页面应由分页文件支持。

也许可以通过解释以下两个方面来回答这个问题:

预订做什么?

提交做什么?

更新-1 11:23 PM 1:48 PM

以下是《 Windows通过C / C ++第5版》的引文,使我感到困惑。

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

那么,RAM在哪里?如果我将机器配置为没有页面文件怎么办?

保留页面的全部目的是确保连续的地址空间可用于某些任务。例如,我们希望堆栈能够增长到1MB,但是我们不想提交所有内存,因为它实际上尚未使用。因此,我们保留1MB的页面,但只提交少量的内容,例如64kB。通过在提交区域的末尾设置保护页面,我们可以检测何时需要提交更多的内存。

提交内存是将某种类型的存储映射到页面的行为。它可以位于物理RAM(它是工作集的一部分)中,也可以位于页面文件中。它也可以映射到私有内存中。为了方便起见,NtAllocateVirtualMemory / VirtualAlloc可以同时保留和提交。

编辑更新的问题:提交页面时,将按进程页面文件配额/系统范围的承诺限制收费。此限制取决于可用的物理RAM数量和页面文件的大小。这实际上并不意味着页面是存储在页面文件中或写入页面文件中。如果内存不足,则可能是这种情况,但是其他情况下,页面大多保留在物理内存中。

您实际上不必遵循2阶段的预留/提交分配方案。

关键是VirtualAlloc和VirtualFree可能会做几件事。有时这样做确实很有用。但是您不必。

承诺内存区域是系统为其分配物理存储的区域。

您不必担心它的确切分配位置:RAM或页面文件。这对您应该是透明的(除非您正在编写内核模式设备驱动程序)。而且,大多数存储页面都可以换出到页面文件中,并按需加载到RAM中。

只是一些已提交的内存页面不需要与页面文件绑定,因为它们已经绑定到另一个物理存储。内存映射文件就是这样的示例。

在正常情况下,当您提交内存页面时,请使用一段时间并释放它-最有可能根本无法到达页面文件。

相关讨论 物理存储不是页面文件,它不是RAM还是页面文件。 使操作系统提交内存区域的唯一方法是读取/写入页面。 内存映射文件也可以是一种物理存储。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3