简体   繁体   English

如何在linux上找到C ++程序的最大内存堆栈大小? (gnu编译器)

[英]How can you find out the maximum size of the memory stack for a C++ program on linux? (gnu compiler)

I am curious about how to find out what the maximum stack size is for a particular compiler/os combo. 我很好奇如何找出特定编译器/ os组合的最大堆栈大小。 I am using Ubuntu/GNU compiler. 我正在使用Ubuntu / GNU编译器。 A few questions I have in addition are: 我还有几个问题:

  1. Who controls the default maximum stack size; 谁控制默认的最大堆栈大小; OS or compiler? OS还是编译器?
  2. Is the default maximum scaled according to total memory? 默认最大值是否根据总内存进行缩放? (ie a machine with 2gb memory would have larger default size than a machine with only 512mb) For this example both machines are same os/compiler setup, just different amounts of system RAM. (即具有2GB内存的机器将具有比仅具有512mb的机器更大的默认大小)对于该示例,两个机器是相同的操作系统/编译器设置,只是不同数量的系统RAM。

Thanks! 谢谢!

Who controls the default maximum stack size; 谁控制默认的最大堆栈大小; OS or compiler? OS还是编译器?

The compiler typically. 通常是编译器。 The OS/hardware does limit it to a certain extent. OS /硬件确实在一定程度上限制了它。 Default is 8MB on linux IIRC. linux IIRC上默认为8MB。 Think of ulimit -s on Linux (to change stack sizes). 想想Linux上的ulimit -s (改变堆栈大小)。

Is the default maximum scaled according to total memory? 默认最大值是否根据总内存进行缩放? (ie a machine with 2gb memory would have larger default size than a machine with only 512mb) For this example both machines are same os/compiler setup, just different amounts of system RAM. (即具有2GB内存的机器将具有比仅具有512mb的机器更大的默认大小)对于该示例,两个机器是相同的操作系统/编译器设置,只是不同数量的系统RAM。

No. Until and unless you do it yiurself.You can alter stack sizes via compiler switches. 不会。除非你自己做,否则你可以通过编译器开关改变堆栈大小。

ld --stack=<STACK_SIZE>

or 要么

gcc -Wl,--stack=<STACK_SIZE>

The C++ Standard's take on the issue of stacks and heaps: C ++标准涉及堆栈和堆的问题:

The standard is based on an abstract machine and does not really concern itself with hardware or stacks or heaps. 该标准基于抽象机器,并不真正涉及硬件或堆栈或堆。 It does talk about an allocated store and a free store. 它确实讨论了分配的商店和免费商店。 The free store is where you'd be if you are calling new (mostly). 如果你打电话给新人(大部分),免费商店就是你的所在地。 FWIW, an implementation can have only one memory area masquerading as both stack and heap when it comes to object allocation. FWIW,在对象分配方面,一个实现只能有一个伪装成堆栈和堆的内存区域。

Your question, therefor, boils down to be an implementation specific issue rather than a language issue. 因此,您的问题归结为一个特定于实现的问题,而不是语言问题。

Hope this helps. 希望这可以帮助。

On Linux (Ubuntu), operating system controls the maximum size. 在Linux(Ubuntu)上,操作系统控制最大大小。 See "man limit" or "man ulimit" for reference. 请参阅“man limit”或“man ulimit”以供参考。

Nowadays, the correct question is: How much memory is allocated to my thread. 如今,正确的问题是: 我的线程分配多少内存 Each thread gets an amount which typically you can control at thread-creation time. 每个线程都获得一个通常可以在创建线程时控制的数量。

To answer part 1, the compiler / thread-system gets to pick, although some OS's have (historically) had limits. 为了回答第1部分,编译器/线程系统可以选择,尽管某些操作系统(历史上)有限制。

For 2, no it's not scaled. 对于2,不,它没有缩放。

没有办法这样做 - C ++标准实际上并不需要堆栈。

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

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