C++ 如何增加堆栈/堆上分配的最大内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13944841/
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-08-27 17:50:36  来源:igfitidea点击:

How to increase the maximum memory allocated on the stack/heap

c++memory

提问by Computernerd

  • How can one increase the maximum memory allocated on the stack/heap for a program in C++?

  • Will increasing the RAM of your computer automatically increase the stack/heap memory of a computer program?

  • 如何增加在 C++ 程序的堆栈/堆上分配的最大内存?

  • 增加计算机的 RAM 会自动增加计算机程序的堆栈/堆内存吗?

回答by VeLKerr

In Visual C++ you may use directive #pragma. For example:

在 Visual C++ 中,您可以使用指令#pragma。例如:

#pragma comment(linker, "/STACK:2000000")
#pragma comment(linker, "/HEAP:2000000")

回答by Joseph Quinsey

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 Studioand C/C++ maximum stack size of program.

第二次编辑:我从你的评论中看到你在 Windows 中工作,所以我下面的 Unix 答案对你没有太大帮助。但请参阅Determining Stack Space with Visual StudioC/C++ 程序的最大堆栈大小

The stacksize 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 将给出当前值,以千字节为单位。您可以在(通常)文件 /etc/security/limits.conf 中更改默认值。

You can also, depending on privileges, change it on a per-process basis using setrlimit(). See for example my answerto Segmentation fault: Stack allocation in a C program in Ubuntu when bufffer>4M.

您还可以根据权限使用setrlimit(). 例如,请参阅Segmentation fault: Stack allocation in a C program in Ubuntu when bufffer>4M 的回答

For heap, see e.g Heap size limitation in C. But I don't believe you can increase or decrease the maximum size.

对于,请参见例如C 中的堆大小限制。但我不相信你可以增加或减少最大尺寸。

回答by Matt

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 中的链接选项 /Heap 和 /Stack 指定保留的堆大小和堆栈大小。有关详细信息,请查看这些 MSDN 文章:

  1. Heap Allocation
  2. Stack Allocation
  1. 堆分配
  2. 堆栈分配

回答by brian beuning

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。

回答by Pabitra Dash

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 Sizeand Stack Reserve Size.
  • 打开项目的属性页对话框。
  • 单击链接器文件夹。
  • 单击系统属性页。
  • 修改Heap Reserve SizeStack Reserve Size