用于虚拟内存管理的新Windows 8.1 API:`DiscardVirtualMemory()`vs`VirtualAlloc()`和`MEM_RESET`和`MEM_RESET_UNDO` [英] New Windows 8.1 APIs for virtual memory management: `DiscardVirtualMemory()` vs `VirtualAlloc()` and `MEM_RESET` and `MEM_RESET_UNDO`

查看:493
本文介绍了用于虚拟内存管理的新Windows 8.1 API:`DiscardVirtualMemory()`vs`VirtualAlloc()`和`MEM_RESET`和`MEM_RESET_UNDO`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 8.1 / Server 2012RC2刚刚推出了用于虚拟内存管理的新API: OfferVirtualMemory() ReclaimVirtualMemory() DiscardVirtualMemory(),它们的用法非常简单,只需查看他们的名字即可。

Windows 8.1/Server 2012RC2 just introduced new APIs for virtual memory management: OfferVirtualMemory(), ReclaimVirtualMemory(), DiscardVirtualMemory(), which their usage is pretty straightforward, just by looking at their names.

不能得到这些API是如何工作对 VirtualAlloc()加上标志 MEM_RESET

The thing I can't get is how these APIs work against VirtualAlloc() plus the flags MEM_RESET and MEM_RESET_UNDO, and what are the subtle differences.

对于 OfferVirtualMemory(), MSDN说,它非常类似于 VirtualAlloc() + MEM_RESET ,除了它从工作集中删除页面,

For the OfferVirtualMemory(), MSDN says that is very similar to VirtualAlloc()+MEM_RESET, except that it removes pages from the working set, and restrict further accesses to the pages.

因此,基本上它限制对页面的访问,如果我想再次访问这些页面,我必须调用 ReclaimVirtualMemory(),这很好,但是 MEM_RESET 也会从工作集中删除页面?
不应 MEM_RESET 作为 madvise(2)的POSIX MADV_DONTNEED ,它基本上从进程的页表中删除页面,如果我以后再次访问这些页面,访问将产生一个软错误,这些页面将被重新分配,初始化为零。

So, basically it restricts accesses to the pages, and if I want to access those pages again I must call ReclaimVirtualMemory(), and that's fine, but shouldn't MEM_RESET also removes pages from the working set? Shouldn't MEM_RESET act as the POSIX MADV_DONTNEED flag of madvise(2) which basically removes pages from the process' pages tables, and if I access those pages again in the future, the access will generate a soft-fault, and those pages will be reallocated again, initialized to zero.

如果这是真的,当然页面从进程的工作集中删除,因为它们基本上被释放,即使进程保持虚拟地址分配,

If this is true, of course pages are removed from the working set of the process, because they basically get deallocated, even if the process keeps the virtual addresses allocated, and see them "committed".

现在,让我们看看 DiscardVirtualMemory():这里MSDN没有说明 MEM_RESET 标志,但如果我阅读此API的说明,它似乎真的与 VirtualAlloc c $ c> + MEM_RESET

Now, let's see DiscardVirtualMemory(): here MSDN says nothing about MEM_RESET flag, but if I read the description of this API, it seems really the same thing as VirtualAlloc()+MEM_RESET.

因此,有没有人知道这些API之间有一些区别,这些细微差别的适当用例是什么?
如果他们引入了一个全新的API,如 DiscardVirtualMemory(),应该与旧的方法有一些区别。

So, does anyone know if there are some differences between those APIs, and what are the proper use cases of those subtle differences? IF they introduced a comopletely new API like DiscardVirtualMemory(), there should be some difference with the old method.

如果我想从POSIX移植一个使用 madvise(2) MADV_DONTNEED MADV_WILLNEED ,什么是模仿这个POSIX行为的最好的方法?到目前为止,我使用 VirtualAlloc() + MEM_RESET MADV_DONTNEED VirtualAlloc() + MEM_RESET_UNDO ,用于 MADV_WILLNEED

If I want to port an application from POSIX which uses madvise(2) with MADV_DONTNEED and MADV_WILLNEED, what is the best way to mimic this POSIX behavior? Until now, I used VirtualAlloc()+MEM_RESET for MADV_DONTNEED and VirtualAlloc()+MEM_RESET_UNDO for MADV_WILLNEED. Is that ok, or I can do better with those new APIs?

推荐答案

MSDN上的DiscardVirtualMemory


当应用程序再次访问内存区域时,将恢复
后备RAM,并且内存的内容未定义。

When the region of memory is again accessed by the application, the backing RAM is restored, and the contents of the memory is undefined.

如果我在行之间读取,说:

If I read between the lines, that says,


  • 当您访问

  • 时,会在这些页面提交时向您提供垃圾初始化页面
  • $ b
  • some of what was there might still be there when you access it
  • it is allowed to give you garbage initialized pages when those pages commit
  • if the machine was actually constrained for memory, some pages will probably be zero initialized sometimes

这不是什么意思如果您使用较旧的API重置虚拟地址范围,则会发生。在这种情况下,当您稍后访问这些页面时,可以保证提供零初始化页面。

This is not what would happen if you reset the virtual address range using the older API. In that case, there is a guarantee to provide zero initialized pages when you access those pages later.

这使得Windows可以减少零页面池,当程序想要非常好,并告诉窗口,它可以抛弃一些释放的内存范围。

This enables the possibility of windows putting less pressure on the pool of zeroed pages, when programs want to be extra nice, and tell windows that it can throw away some freed range of memory.

这篇关于用于虚拟内存管理的新Windows 8.1 API:`DiscardVirtualMemory()`vs`VirtualAlloc()`和`MEM_RESET`和`MEM_RESET_UNDO`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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