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

5

13

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?

Hodosh answered 16/4, 2011 at 12:28 Comment(0)
E
15

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.

Eugenioeugenius answered 16/4, 2011 at 12:36 Comment(0)
V
5

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.

Virilism answered 16/4, 2011 at 12:36 Comment(0)
B
4

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

Braxton answered 16/4, 2011 at 12:36 Comment(1)
Actually 32-bit x86 systems with PAE (physical address extensions) can address more than that. If you look more closely at the tables you linked to you'll notice that the 32-bit release of Windows Server 2008 can handle up to 64 GB.Kuhn
V
3

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.

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.

Volteface answered 16/4, 2011 at 12:54 Comment(3)
Wow, it's like it's 1985 all over again!Virilism
@Anon: How do you figure that? Because there are memory limits? That's never going to go away.Doyen
@Cody Gray - the mid-1980s had several approaches to getting past the 640k limit of DOS. One approach used special boards that would switch relatively small banks of memory into a reserved space. Another worked only on the 80286, and would actually switch it into protected mode, copy a block of memory into the low address space, then switch it back into real mode. Google for "DOS expanded memory"Virilism
A
2

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

Arthralgia answered 16/4, 2011 at 12:35 Comment(1)
On 64 bit Windows, a 32 bit app marked as Large Address aware gets almost 4Gb, not 3Gb.Second

© 2022 - 2024 — McMap. All rights reserved.