简体   繁体   English

linux中进程的堆栈大小是否有限制

[英]Is there a limit of stack size of a process in linux

Is there a limit on the stack size of a process in Linux ? Linux进程的stack大小是否有限制? Is it simply dependent on the RAM of the machine? 它只是依赖于机器的RAM吗? I want to know this in order to limit the depth of recursive calls to a function. 我想知道这一点,以限制函数的递归调用的深度。

Thank you. 谢谢。

The stack is normally limited by a resource limit. 堆栈通常受资源限制的限制。 You can see what the default settings are on your installation using ulimit -a : 您可以使用ulimit -a查看安装的默认设置:

stack size              (kbytes, -s) 8192

(this shows that mine is 8MB, which is huge). (这表明我的是8MB,这是巨大的)。

If you remove or increase that limit, you still won't be able to use all the RAM in the machine for the stack - the stack grows downward from a point near the top of your process's address space, and at some point it will run into your code, heap or loaded libraries. 如果删除或增加该限制,您仍然无法使用机器中的所有RAM作为堆栈 - 堆栈从进程地址空间顶部附近的点向下增长,并且在某些时候它将运行进入你的代码,堆或加载的库。

The limit can be set by the admin. 该限制可以由管理员设置。

See man ulimit . 男子ulimit

There is probably a default which you cannot cross. 可能存在您无法跨越的默认值。 If you have to worry about stack limits, I would say you need to rethink your design, perhaps write an iterative version? 如果你不得不担心堆栈限制,我会说你需要重新考虑你的设计,或者写一个迭代版本?

It largely depends what architecture you're on (32 or 64-bit) and whether you're multithreaded or not. 它在很大程度上取决于你所使用的架构(32或64位)以及你是否是多线程的。

By default in a single threaded process, ie the main thread created by the OS at exec() time, your stack usually will grow until it hits something else in the address space. 默认情况下,在单线程进程中,即操作系统在exec()时创建的主线程,您的堆栈通常会增长,直到它触及地址空间中的其他内容。 This means that it is generally possible, on a 32-bit machine, to have, say 1G of stack. 这意味着在32位机器上通常可以使用1G的堆栈。

However, this is definitely NOT the case in a multithreaded 32-bit process. 但是,在多线程32位进程中绝对不是这种情况。 In multithreaded procesess, the stacks share address space and hence need to be allocated, so they typically get given a small amount of address space (eg 1M) so that many threads can be created without exhausting address space. 在多线程过程中,堆栈共享地址空间,因此需要进行分配,因此它们通常会获得少量的地址空间(例如1M),这样就可以创建许多线程而不会耗尽地址空间。

So in a multithreaded process, it's small and finite, in a single threaded one, it's basically until you hit something else in the address-space (which the default allocation mechanism tries to ensure doesn't happen too soon). 因此,在多线程进程中,它是小而有限的,在单线程进程中,它基本上直到你在地址空间中找到其他东西(默认分配机制试图确保不会太快发生)。

In a 64-bit machine, of course there is a lot more address space to play with. 在64位机器中,当然还有更多的地址空间可供使用。

In any case you can always run out of virtual memory, in which case you'll get a SIGBUS or SIGSEGV or something. 在任何情况下,您总是可以用完虚拟内存,在这种情况下,您将获得SIGBUS或SIGSEGV等。

Would have commented on the accepted answer but apparently I need more rep.... 会对已接受的答案发表评论,但显然我需要更多代表....

True Stack Overflow can be subtle and not always cause any error messages or warnings. True Stack Overflow可能很微妙,并不总是导致任何错误消息或警告。 I just had a situation where the only symptom was that socket connections would fail with strange SSL errors. 我只是遇到一种情况,即唯一的症状是套接字连接会因奇怪的SSL错误而失败。 Everything else worked fine. 其他一切都很好。 Threads could malloc(), grab locks, talk to the DB, etc. But new connections would fail at the SSL layer. 线程可以是malloc(),获取锁定,与数据库通信等等。但是新连接在SSL层会失败。

With stack traces from well within GnuTLS I was quite confused about the true cause. 由于GnuTLS中的堆栈跟踪,我对真正的原因感到非常困惑。 Nearly reported the traces to their team after spending lots of time trying to figure it out. 在花了很多时间试图解决问题之后,几乎向他们的团队报告了这些痕迹。

Eventually found that the stacksize was set to 8Mb and immediately upon raising it the problems vanished. 最终发现stacksize设置为8Mb,并在提升后立即消失。 Lowering the stack back to 8Mb brought the problem back (ABA). 将堆栈降低到8Mb会导致问题恢复(ABA)。

So if you are troubleshooting what appears to be strange socket errors without any other warnings or uninitialized memory errors.... it could be stack overflow. 因此,如果您正在排除看似奇怪的套接字错误而没有任何其他警告或未初始化的内存错误....它可能是堆栈溢出。

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

相关问题 限制stl堆栈对象的大小 - Limit on size of stl stack object XCode:多维数组上的堆栈大小限制 - XCode: Stack Size Limit on Multidimensional Array C ++中的堆栈实现,没有最大堆栈大小限制 - Stack implementation in c++ with no maximum stack size limit 在Linux上为程序设置全局默认堆栈大小 - Setting the default stack size on Linux globally for the program Linux 进程的默认分配堆大小 - default allocated heap size for a Linux process Linux 32位计算机上程序的堆栈分配限制 - Stack allocation limit for programs on a Linux 32 bit machine 如何使用Win32 API获取任何线程的堆栈大小和堆栈限制 - How to get stack size and stack limit of any thread using Win32 API 在Linux平台上用c / c ++打印所有线程堆栈进程的跟踪 - print all threads stack trace of a process in c/c++ on linux platform 有什么方法可以确定一个(多)线程/任务的堆栈地址在 Linux 上的进程的虚拟 memory 中开始? - Is there any way to determine a (multi) thread/task's stack address start in the virtual memory of a process on Linux? Linux :: glibc中的C ++运行时错误检测到free():下一个大小无效(快速):带有无效ptr元素的堆栈 - C++ runtime error in Linux ::glibc detected free(): invalid next size (fast): Stack with void ptr element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM