32 位 Windows 上的 C++ 应用程序可用的最大内存是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5686459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:39:55  来源:igfitidea点击:

What is the maximum memory available to a C++ application on 32-bit Windows?

c++windows32-bitmemory-limit

提问by Hyman Kada

Just wondering if there is a restriction on the max memory that a C++ application uses

只是想知道 C++ 应用程序使用的最大内存是否有限制

I understand that this is 2GB - Is that correct?

我知道这是 2GB - 对吗?

If a C++ app tries to request more then 2GB memory does this cause a memory crash?

如果 C++ 应用程序尝试请求超过 2GB 的内存,这是否会导致内存崩溃?

Final question - If the machine the C++ app is running on is already low on memory and a C++ app asks for 100MB of array (ie contiguous memory) will the OS accommodate this by using virtual memory?

最后一个问题 - 如果运行 C++ 应用程序的机器内存已经很低,并且 C++ 应用程序要求 100MB 的数组(即连续内存),操作系统是否会通过使用虚拟内存来适应这一点?

回答by Puppy

It will cause a dynamic memory allocation failure, which usually will make the resultant application crash, but technically, an application could be written to withstand this event. 2GB is indeed the user address space size for an individual process- an application may use multiple processes (easiest example: Chrome). If an application asks for 100MB of contiguous memory, that memory mustbe virtually contiguous even if not physically contiguous, and if there aren't enough contiguous pages available then it's a failed allocation.

它会导致动态内存分配失败,这通常会使结果应用程序崩溃,但从技术上讲,可以编写应用程序来承受此事件。2GB 确实是单个进程的用户地址空间大小——一个应用程序可以使用多个进程(最简单的例子:Chrome)。如果应用程序要求 100MB 的连续内存,那么即使不是物理上连续的内存也必须是虚拟连续的,如果没有足够的连续页面可用,则分配失败。

Virtual memory is always used- all memory is virtual.

始终使用虚拟内存 - 所有内存都是虚拟的。

2GB is the limit under most circumstances. What happens is that normally, 2GB is for the user and 2GB for the kernel, but you canask Windows to make this 3GB for the user and 1GB for the kernel (at some risk), and on 64bit, the whole 4GB of 32bit address space is available to the user. The increased address space is only available if you compile your application as /LARGEADDRESSAWARE.

2GB 是大多数情况下的限制。发生的情况是,通常情况下,2GB 用于用户,2GB 用于内核,但是您可以要求 Windows 为用户提供 3GB,为内核提供 1GB(有一定风险),在 64 位上,整个 4GB 的 32 位地址空间可供用户使用。只有将应用程序编译为/LARGEADDRESSAWARE.

回答by Anon

The restriction depends on the operating system. Standard Linux is 2 Gb, Solaris is 3 Gb, Windows is (I'm told) 2 or 3 depending on how PAE is used.

该限制取决于操作系统。标准 Linux 是 2 Gb,Solaris 是 3 Gb,Windows 是(我听说)2 或 3,具体取决于 PAE 的使用方式。

However, you don't get all of that 2G for your data. Your code will take some of it, and your program's stack will take some, and the C library will take some, as will any other shared libraries that you reference. Typically the OS will organize the code, heap, and stack such that there are intentional gaps between them.

但是,您无法获得所有 2G 的数据。您的代码将使用其中一些,您的程序堆栈将使用一些,C 库将使用一些,您引用的任何其他共享库也将使用。通常操作系统会组织代码、堆和堆栈,以便在它们之间存在有意的间隙。

As for your final question: it's all virtual memory. What you are actually asking is "if the programs in my machine use all that physical memory, will the OS use swap." And the answer is yes, but not quite the way you think.

至于你的最后一个问题:这都是虚拟内存。您实际上要问的是“如果我机器中的程序使用所有物理内存,操作系统是否会使用交换。” 答案是肯定的,但并不完全是你想的那样。

A CPU can only access physical RAM. It knows nothing of data stored on disk. So to give physical memory to a running process, the OS will take that memory from another process. In order to take the memory, it will write it to swap. When that other process needs to access the memory, the OS will read it back in, potentially writing some other process' memory to swap.

CPU 只能访问物理 RAM。它对存储在磁盘上的数据一无所知。因此,为了给正在运行的进程提供物理内存,操作系统将从另一个进程中获取该内存。为了获取内存,它会将它写入交换。当其他进程需要访问内存时,操作系统会将其读回,可能会写入其他进程的内存以进行交换。

回答by Chris Pitman

Although the other answers are correct in the usual case, there is support in Windows XP 32 bit to use way more than 3GB of memory by using Address Windowing Extensions.

尽管其他答案在通常情况下是正确的,但 Windows XP 32 位支持通过使用Address Windowing Extensions来使用超过 3GB 的内存。

AWE is commonly used by database servers to enable them to access extremely large sets of memory. It requires using the Win API to actually manage the memory, so it is obviously bes to use only when really needed.

数据库服务器通常使用 AWE 使它们能够访问极大的内存集。它需要使用 Win API 来实际管理内存,因此显然只能在真正需要时使用。

回答by NPE

Typically, a 32-bit OS can only address 4GB of physical RAM. In practice this limit tends to be somewhat lower, but can be alleviated with the use of virtual RAM. On certain versions of Windows it can be increased through the use of Physical Address Extension.

通常,32 位操作系统只能处理 4GB 的物理 RAM。在实践中,这个限制往往会更低一些,但可以通过使用虚拟 RAM 来缓解。在某些版本的 Windows 上,它可以通过使用物理地址扩展来增加。

More importantly to your question, on 32-bit Windows there is also a 2GB limit on the address spaceavailable to a user application. This places a hard constraint on the amount of memory that a single application can use, irrespective of the amount of physical or virtual RAM available. The default 2GB limit can be increased to 3GB.

对您的问题更重要的是,在 32 位 Windows 上,用户应用程序可用的地址空间也有 2GB 的限制。这对单个应用程序可以使用的内存量施加了严格的限制,而与可用的物理或虚拟 RAM 量无关。默认的 2GB 限制可以增加到 3GB。

The following page explains the limits in detail: http://msdn.microsoft.com/en-us/library/aa366778(v=vs.85).aspx

以下页面详细解释了限制:http: //msdn.microsoft.com/en-us/library/aa366778(v=vs.85).aspx

回答by Erik

All the memory you have access to is virtual - you cannot access physical memory directly from an application. The OS will use the page file as needed - the effect you'll see by having many applications exhausting physical memory is increased swapping, and noticable slowdown.

您可以访问的所有内存都是虚拟的 - 您不能直接从应用程序访问物理内存。操作系统将根据需要使用页面文件 - 您会看到许多应用程序耗尽物理内存的效果是交换增加,并且速度明显减慢。

On Win 32 bit, the application has 2GB of Virtual Address Spaceavailable. This is used for mapping executables and DLLs, for e.g. memory-mapped files, for stack and heap. This space is typically somewhat fragmented. If your application is built as "Large Address Aware", and the OS is 64-bit or configured to split user/kernel-mode memory as 3/1GB, the address space is almost 4GB for 64-bit, and 3GB for 32-bit.

在 Win 32 位上,该应用程序有 2GB 的可用虚拟地址空间。这用于映射可执行文件和 DLL,例如内存映射文件、堆栈和堆。这个空间通常有点碎片化。如果您的应用程序构建为“大地址感知”,并且操作系统为 64 位或配置为将用户/内核模式内存拆分为 3/1GB,则 64 位的地址空间几乎为 4GB,32 位为 3GB少量。

The memory you can allocateis typically in the 17-1800 MB range. If you allocate small portions, you'll reach this, if you try to allocate large consecutive blocks you may hit the limit a lot earlier, as your address space is fragmented.

您可以分配的内存通常在 17-1800 MB 范围内。如果你分配小部分,你会达到这个,如果你尝试分配大的连续块,你可能会更早地达到限制,因为你的地址空间是碎片化的。

See e.g. Virtual Address Space on MSDNor Virtual Address Space on Wikipedia

参见例如MSDN 上的虚拟地址空间维基百科上的虚拟地址空间