Just wondering if there is a restriction on the max memory that a C++ application uses
I understand that this is 2GB - Is that correct?
If a C++ app tries to request more then 2GB memory does this cause a memory crash?
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?
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.
More importantly to your question, on 32-bit Windows there is also a 2GB limit on the address space available 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.
The following page explains the limits in detail: 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. 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.
The memory you can allocate is 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.
See e.g. Virtual Address Space on MSDN or Virtual Address Space on Wikipedia
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 must be virtually contiguous even if not physically contiguous, and if there aren't enough contiguous pages available then it's a failed allocation.
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 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. The increased address space is only available if you compile your application as /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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With