-
How can one increase the maximum memory allocated on the stack/heap for a program in C++?
如何在C ++中增加程序在堆栈/堆上分配的最大内存?
-
Will increasing the RAM of your computer automatically increase the stack/heap memory of a computer program?
增加计算机的RAM会自动增加计算机程序的堆栈/堆内存吗?
5 个解决方案
#1
8
In Visual C++ you may use directive #pragma
. For example:
在Visual C ++中,您可以使用指令#pragma。例如:
#pragma comment(linker, "/STACK:2000000")
#pragma comment(linker, "/HEAP:2000000")
#2
5
You can specify the reserved heap size and stack size with the link options /Heap and /Stack in Visual Studio. For details, check these MSDN articles:
您可以使用Visual Studio中的链接选项/堆和/堆栈指定保留的堆大小和堆栈大小。有关详细信息,请查看以下MSDN文章:
#3
5
Second edit: I see from your comment that you work in Windows, so my Unix answer below would not be very helpful to you. But see Determining Stack Space with Visual Studio and C/C++ maximum stack size of program.
第二次编辑:我从你的评论中看到你在Windows中工作,所以下面我的Unix答案对你没有多大帮助。但请参阅使用Visual Studio和C / C ++确定堆栈空间最大堆栈大小的程序。
The stack size is quite often limited in Linux. The command ulimit -s will give the current value, in Kbytes. You can change the default in (usually) the file /etc/security/limits.conf.
在Linux中,堆栈大小通常是有限的。命令ulimit -s将以Kbytes为单位给出当前值。您可以(通常)更改文件/etc/security/limits.conf中的默认值。
You can also, depending on privileges, change it on a per-process basis using setrlimit()
. See for example my answer to Segmentation fault: Stack allocation in a C program in Ubuntu when bufffer>4M.
您还可以根据权限使用setrlimit()在每个进程的基础上更改它。例如,参见我对Segmentation故障的回答:当bufffer> 4M时,在Ubuntu的C程序中进行堆栈分配。
For heap, see e.g Heap size limitation in C. But I don't believe you can increase or decrease the maximum size.
对于堆,请参阅例如C中的堆大小限制。但我不相信您可以增加或减少最大大小。
#4
3
The heap size of a process is usually limited by the maximum memory the process can allocate. The heap does not need to be contiguous (unless you are doing something like malloc(1000000000)) so the heap can use most of the available address space.
进程的堆大小通常受进程可分配的最大内存限制。堆不需要是连续的(除非您正在执行类似malloc(1000000000)的操作),因此堆可以使用大部分可用的地址空间。
Under Windows the maximum process size varies by a couple of factors.
在Windows下,最大处理大小因几个因素而异。
Using 32-bit Windows, a 32-bit process can by default allocate 2 GB. If Windows is booted using the /3GB switch and the process is compiled using the "Enable Large Addresses" linker flag, then the process can allocate 3 GB.
使用32位Windows,32位进程默认情况下可以分配2 GB。如果使用/ 3GB开关启动Windows并使用“启用大地址”链接器标志编译该进程,则该进程可以分配3 GB。
Using 64-bit Windows, a 32-bit process by default can allocate 2 GB. If the process is linked with "Enable Large Addresses", then 64-bit Windows lets a 32-bit process allocate 4 GB.
使用64位Windows,默认情况下32位进程可以分配2 GB。如果该进程与“启用大地址”链接,则64位Windows允许32位进程分配4 GB。
A 64-bit process (on 64-bit Windows) can allocate something like 16,000 GB.
64位进程(在64位Windows上)可以分配16,000 GB的内容。
#5
1
I was getting some memory issue with default heap and stack reserve size of 1 MB. But when I set above properties to 2 MB (2000000), it works fine.
我得到一些内存问题,默认堆和堆栈保留大小为1 MB。但是当我将上面的属性设置为2 MB(2000000)时,它工作正常。
To set these properties in the Visual Studio development environment, please follow steps below.
要在Visual Studio开发环境中设置这些属性,请按照以下步骤操作。
- Open the project's Property Pages dialog box.
- Click the Linker folder.
- Click the System property page.
- Modify
Heap Reserve Size
andStack Reserve Size
.
打开项目的“属性页”对话框。
单击“链接器”文件夹。
单击“系统”属性页。
修改堆保留大小和堆栈保留大小。
#1
8
In Visual C++ you may use directive #pragma
. For example:
在Visual C ++中,您可以使用指令#pragma。例如:
#pragma comment(linker, "/STACK:2000000")
#pragma comment(linker, "/HEAP:2000000")
#2
5
You can specify the reserved heap size and stack size with the link options /Heap and /Stack in Visual Studio. For details, check these MSDN articles:
您可以使用Visual Studio中的链接选项/堆和/堆栈指定保留的堆大小和堆栈大小。有关详细信息,请查看以下MSDN文章:
#3
5
Second edit: I see from your comment that you work in Windows, so my Unix answer below would not be very helpful to you. But see Determining Stack Space with Visual Studio and C/C++ maximum stack size of program.
第二次编辑:我从你的评论中看到你在Windows中工作,所以下面我的Unix答案对你没有多大帮助。但请参阅使用Visual Studio和C / C ++确定堆栈空间最大堆栈大小的程序。
The stack size is quite often limited in Linux. The command ulimit -s will give the current value, in Kbytes. You can change the default in (usually) the file /etc/security/limits.conf.
在Linux中,堆栈大小通常是有限的。命令ulimit -s将以Kbytes为单位给出当前值。您可以(通常)更改文件/etc/security/limits.conf中的默认值。
You can also, depending on privileges, change it on a per-process basis using setrlimit()
. See for example my answer to Segmentation fault: Stack allocation in a C program in Ubuntu when bufffer>4M.
您还可以根据权限使用setrlimit()在每个进程的基础上更改它。例如,参见我对Segmentation故障的回答:当bufffer> 4M时,在Ubuntu的C程序中进行堆栈分配。
For heap, see e.g Heap size limitation in C. But I don't believe you can increase or decrease the maximum size.
对于堆,请参阅例如C中的堆大小限制。但我不相信您可以增加或减少最大大小。
#4
3
The heap size of a process is usually limited by the maximum memory the process can allocate. The heap does not need to be contiguous (unless you are doing something like malloc(1000000000)) so the heap can use most of the available address space.
进程的堆大小通常受进程可分配的最大内存限制。堆不需要是连续的(除非您正在执行类似malloc(1000000000)的操作),因此堆可以使用大部分可用的地址空间。
Under Windows the maximum process size varies by a couple of factors.
在Windows下,最大处理大小因几个因素而异。
Using 32-bit Windows, a 32-bit process can by default allocate 2 GB. If Windows is booted using the /3GB switch and the process is compiled using the "Enable Large Addresses" linker flag, then the process can allocate 3 GB.
使用32位Windows,32位进程默认情况下可以分配2 GB。如果使用/ 3GB开关启动Windows并使用“启用大地址”链接器标志编译该进程,则该进程可以分配3 GB。
Using 64-bit Windows, a 32-bit process by default can allocate 2 GB. If the process is linked with "Enable Large Addresses", then 64-bit Windows lets a 32-bit process allocate 4 GB.
使用64位Windows,默认情况下32位进程可以分配2 GB。如果该进程与“启用大地址”链接,则64位Windows允许32位进程分配4 GB。
A 64-bit process (on 64-bit Windows) can allocate something like 16,000 GB.
64位进程(在64位Windows上)可以分配16,000 GB的内容。
#5
1
I was getting some memory issue with default heap and stack reserve size of 1 MB. But when I set above properties to 2 MB (2000000), it works fine.
我得到一些内存问题,默认堆和堆栈保留大小为1 MB。但是当我将上面的属性设置为2 MB(2000000)时,它工作正常。
To set these properties in the Visual Studio development environment, please follow steps below.
要在Visual Studio开发环境中设置这些属性,请按照以下步骤操作。
- Open the project's Property Pages dialog box.
- Click the Linker folder.
- Click the System property page.
- Modify
Heap Reserve Size
andStack Reserve Size
.
打开项目的“属性页”对话框。
单击“链接器”文件夹。
单击“系统”属性页。
修改堆保留大小和堆栈保留大小。