简体   繁体   English

为什么在Windows Vista x64上调用GlobalMemoryStatus时可用物理内存(dwAvailPhys)>可用虚拟内存(dwAvailVirtual)

[英]Why is Available Physical Memory (dwAvailPhys) > Available Virtual Memory (dwAvailVirtual) in call GlobalMemoryStatus on Windows Vista x64

I am playing with an MSDN sample to do memory stress testing (see: http://msdn.microsoft.com/en-us/magazine/cc163613.aspx ) and an extension of that tool that specifically eats physical memory (see http://www.donationcoder.com/Forums/bb/index.php?topic=14895.0;prev_next=next ). 我正在使用一个MSDN示例进行内存压力测试(请参阅: http : //msdn.microsoft.com/en-us/magazine/cc163613.aspx )以及该工具的扩展,该工具专门占用了物理内存(请参见http: //www.donationcoder.com/Forums/bb/index.php?topic=14895.0;prev_next=next )。 I am obviously confused though on the differences between Virtual and Physical Memory. 我对虚拟内存和物理内存之间的差异感到很困惑。 I thought each process has 2 GB of virtual memory (although I also read 1.5 GB because of "overhead". My understanding was that some/all/none of this virtual memory could be physical memory, and the amount of physical memory used by a process could change over time (memory could be swapped out to disc, etc.)I further thought that, in general, when you allocate memory, the operating system could use physical memory or virtual memory. From this, I conclude that dwAvailVirtual should always be equal to or greater than dwAvailPhys in the call GlobalMemoryStatus. However, I often (always?) see the opposite. What am I missing. 我以为每个进程都有2 GB的虚拟内存(尽管由于“开销”,我也读了1.5 GB。我的理解是,该虚拟内存的部分/全部/全部可能是物理内存,而某个虚拟机使用的物理内存量)进程可能会随着时间而改变(内存可能会换出到磁盘等)我进一步认为,通常,当您分配内存时,操作系统可以使用物理内存或虚拟内存,由此得出的结论是,dwAvailVirtual应该始终在调用GlobalMemoryStatus中等于或大于dwAvailPhys,但是,我经常(总是?)看到相反的情况,我缺少什么。

I apologize in advance if my question is not well formed. 如果我的提问格式不正确,我谨向您致歉。 I'm still trying to get my head around the whole memory management system in Windows. 我仍在努力探索Windows中的整个内存管理系统。 Tutorials/Explanations/Book recs are most welcome! 欢迎使用教程/说明/书籍记录!

Andrew 安德鲁

That was only true in the olden days, back when RAM was expensive. 那只是在过去,那是在内存昂贵的年代。 The operating system maps pages of virtual memory to RAM as needed. 操作系统根据需要将虚拟内存的页面映射到RAM。 If there isn't enough RAM to satisfy a program's request, it starts unmapping pages to make room. 如果没有足够的RAM来满足程序的请求,它将开始取消映射页面以腾出空间。 If such a page contains data instead of code, it gets written to the paging file. 如果这样的页面包含数据而不是代码,它将被写入页面文件。 Whenever the program accesses that page again, it generates a paging fault, letting the operating system read the page back from disk. 只要程序再次访问该页面,就会产生页面错误,使操作系统从磁盘读回该页面。

If the machine has little RAM and lots of processes consuming virtual memory pages, that can cause a very unpleasant effect called "thrashing". 如果计算机的RAM很少,并且有大量进程占用虚拟内存页面,则可能导致非常令人不快的效果,称为“崩溃”。 The operating system is constantly accessing the disk and machine performance slows down to a crawl. 操作系统不断访问磁盘,计算机性能降低到爬网速度。

More RAM means less disk access. 更多的RAM意味着更少的磁盘访问。 There's very little reason not to use 3 or 4 GB of RAM on a 32-bit operating system, it's cheap. 在32位操作系统上几乎没有理由不使用3或4 GB的RAM,这很便宜。 Even if you do not get to use all 4 GB, not all of it will be addressable due hardware devices taking space on the address bus (video, mostly). 即使您没有使用全部4 GB,由于硬件设备占用了地址总线上的空间(大部分是视频),因此并非全部都能使用。 But that won't change the size of the virtual memory accessible by user code, it is still 2 Gigabytes. 但这不会改变用户代码可访问的虚拟内存的大小,仍然是2 GB。

Windows Internals is a good book. Windows Internals是一本好书。

I don't know if this is your issue, but the MSDN page for the GlobalMemoryStatus function contains the following warning: 我不知道这是否是您的问题,但是GlobalMemoryStatus函数MSDN页面包含以下警告:

On computers with more than 4 GB of memory, the GlobalMemoryStatus function can return incorrect information, reporting a value of –1 to indicate an overflow. 在内存大于4 GB的计算机上, GlobalMemoryStatus函数可能返回错误的信息,报告值–1表示溢出。 For this reason, applications should use the GlobalMemoryStatusEx function instead. 因此,应用程序应改用GlobalMemoryStatusEx函数。

Additionally, that page says: 此外,该页面显示:

On Intel x86 computers with more than 2 GB and less than 4 GB of memory, the GlobalMemoryStatus function will always return 2 GB in the dwTotalPhys member of the MEMORYSTATUS structure. 在内存大于2 GB,内存小于4 GB的Intel x86计算机上, GlobalMemoryStatus函数将始终在MEMORYSTATUS结构的dwTotalPhys成员中返回2 GB。 Similarly, if the total available memory is between 2 and 4 GB, the dwAvailPhys member of the MEMORYSTATUS structure will be rounded down to 2 GB. 同样,如果总可用内存在2-4 GB之间,则MEMORYSTATUS结构的dwAvailPhys成员将向下舍入为2 GB。 If the executable is linked using the /LARGEADDRESSAWARE linker option, then the GlobalMemoryStatus function will return the correct amount of physical memory in both members. 如果使用/ LARGEADDRESSAWARE链接器选项链接了可执行文件,则GlobalMemoryStatus函数将在两个成员中返回正确的物理内存量。

Since you're referring to members like dwAvailPhys instead of ullAvailPhys , it sounds like you're using a MEMORYSTATUS structure instead of a MEMORYSTATUSEX structure. 由于您所指的成员是dwAvailPhys之类的成员,而不是ullAvailPhys ,因此听起来您正在使用MEMORYSTATUS结构而不是MEMORYSTATUSEX结构。 I don't know the consequences of that on a 64-bit platform, but on a 32-bit platform that definitely could cause incorrect memory sizes to be reported. 我不知道在64位平台上这样做的后果,但是在32位平台上肯定会导致报告错误的内存大小。

The amount of virtual memory is limited by size of the address space - which is 4GB per process on a 32-bit system. 虚拟内存的数量受地址空间大小的限制-在32位系统上,每个进程为4GB。 And you have to subtract from this the size of regions reserved for system use and the amount of VM used already by your process (including all the libraries mapped to its address space). 而且,您必须从中减去保留供系统使用的区域大小和进程已使用的VM数量(包括映射到其地址空间的所有库)。

On the other hand, the total amount of physical memory may be higher than the amount of virtual memory space the system has left free for your process to use (and these days it often is). 另一方面,物理内存的总量可能会高于系统为您的进程留出的虚拟内存空间量(现在通常如此)。

This means that if you have more than ~2GB or RAM, you can't use all your physical memory in one process (since there's not enough virtual memory space to map it to), but it can be used by many processes. 这意味着,如果您的内存或内存超过约2GB,则无法在一个进程中使用所有物理内存(因为没有足够的虚拟内存空间可将其映射到),但是可以被许多进程使用。 Note that this limitation is removed in a 64-bit system. 请注意,此限制在64位系统中已删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM