简体   繁体   English

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

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

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的阵列(即连续的内存),操作系统是否会通过虚拟内存来适应这种情况?

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). 2GB确实是单个进程的用户地址空间大小 - 应用程序可能使用多个进程(最简单的示例:Chrome)。 If an application asks for 100MB of contiguous memory, that memory must be virtually contiguous even if not physically contiguous, and if there aren't enough contiguous pages available then it's a failed allocation. 如果应用程序要求100MB的连续内存,即使没有物理上连续的内存,该内存也必须是几乎连续的,并且如果没有足够的连续页面,那么这是一个失败的分配。

Virtual memory is always used- all memory is virtual. 始终使用虚拟内存 - 所有内存都是虚拟内存。

2GB is the limit under most circumstances. 在大多数情况下,2GB是限制。 What happens is that normally, 2GB is for the user and 2GB for the kernel, but you can ask 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. 通常情况下,2GB用于用户,2GB用于内核,但是你可以要求Windows为用户提供3GB,为内核提供1GB(存在一定风险),在64位时,要求整个4GB的32位地址空间可供用户使用。 The increased address space is only available if you compile your application as /LARGEADDRESSAWARE . 只有将应用程序编译为/LARGEADDRESSAWARE才能使用增加的地址空间。

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. 但是,您没有为您的数据获得所有2G。 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. 你的代码会占用一些,你的程序的堆栈会占用一些,而C库会占用一些,你引用的任何其他共享库也会占用一些。 Typically the OS will organize the code, heap, and stack such that there are intentional gaps between them. 通常,操作系统将组织代码,堆和堆栈,以便它们之间存在有意的间隙。

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. CPU只能访问物理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. 当其他进程需要访问内存时,操作系统会将其读回,可能会将其他进程的内存写入交换状态。

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 . 虽然其他答案在通常情况下都是正确的,但是通过使用Address Windowing Extensions ,Windows XP 32位支持使用超过3GB的内存。

AWE is commonly used by database servers to enable them to access extremely large sets of memory. 数据库服务器通常使用AWE来使它们能够访问极大的内存集。 It requires using the Win API to actually manage the memory, so it is obviously bes to use only when really needed. 它需要使用Win API来实际管理内存,所以显然只有在真正需要时才使用它。

Typically, a 32-bit OS can only address 4GB of physical RAM . 通常,32位操作系统只能处理4GB的物理RAM In practice this limit tends to be somewhat lower, but can be alleviated with the use of virtual RAM. 实际上,这个限制往往略低,但可以通过使用虚拟RAM来缓解。 On certain versions of Windows it can be increased through the use of Physical Address Extension . 在某些版本的Windows上,可以通过使用物理地址扩展来增加它。

More importantly to your question, on 32-bit Windows there is also a 2GB limit on the address space available to a user application. 更重要的是,对于您的问题,在32位Windows上,用户应用程序可用的地址空间也有2GB的限制。 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. 这对单个应用程序可以使用的内存量施加了严格的限制,无论可用的物理或虚拟RAM的数量如何。 The default 2GB limit can be increased to 3GB. 默认的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

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 Space available. 在Win 32位上,该应用程序有2GB的虚拟地址空间可用。 This is used for mapping executables and DLLs, for eg memory-mapped files, for stack and heap. 这用于映射可执行文件和DLL,例如用于堆栈和堆的内存映射文件。 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. 如果您的应用程序构建为“大地址识别”,并且操作系统是64位或配置为将用户/内核模式内存拆分为3 / 1GB,则64位的地址空间几乎为4GB,32-的地址空间为3GB位。

The memory you can allocate is typically in the 17-1800 MB range. 您可以分配的内存通常在17-1800 MB范围内。 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. 如果你分配小部分,你会达到这个目标,如果你试图分配大的连续块,你可能会提前很多,因为你的地址空间是碎片化的。

See eg Virtual Address Space on MSDN or Virtual Address Space on Wikipedia 请参阅例如MSDN上的 虚拟地址空间Wikipedia上的虚拟地址空间

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

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