"ulimit -s unlimited"是什么?做? [英] What does "ulimit -s unlimited" do?

查看:3525
本文介绍了"ulimit -s unlimited"是什么?做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于堆栈分配,有许多可以理解的相关问题

There are understandably many related questions on stack allocation

什么是堆栈和堆?在哪里?

为什么对堆栈大小有限制?

堆栈和堆内存的大小

但是在各种* nix机器上,我可以发出bash命令

However on various *nix machines I can issue the bash command

ulimit -s unlimited

或csh命令

set stacksize unlimited

这如何改变程序的执行方式?对程序或系统性能有影响吗(例如,为什么不将其设置为默认值)?

How does this change how programs are executed? Are there any impacts on program or system performance (e.g., why wouldn't this be the default)?

如果需要更多系统详细信息,我主要关注的是在x86_64硬件上运行的Linux上使用GCC编译的程序.

In case more system details are relevant, I'm mostly concerned with programs compiled with GCC on Linux running on x86_64 hardware.

推荐答案

调用函数时,会在堆栈上分配一个新的命名空间".这就是函数可以具有局部变量的方式.作为函数调用函数,而函数又依次调用函数,我们不断在堆栈上分配越来越多的空间来维护这种深层次的名称空间层次结构.

When you call a function, a new "namespace" is allocated on the stack. That's how functions can have local variables. As functions call functions, which in turn call functions, we keep allocating more and more space on the stack to maintain this deep hierarchy of namespaces.

要限制使用大量堆栈空间的程序,通常通过ulimit -s设置限制.如果我们通过ulimit -s unlimited取消该限制,我们的程序将能够继续为其不断增长的堆栈增加RAM,直到最终系统完全耗尽内存.

To curb programs using massive amounts of stack space, a limit is usually put in place via ulimit -s. If we remove that limit via ulimit -s unlimited, our programs will be able to keep gobbling up RAM for their evergrowing stack until eventually the system runs out of memory entirely.

int eat_stack_space(void) { return eat_stack_space(); }
// If we compile this with no optimization and run it, our computer could crash.

通常,使用大量的堆栈空间是偶然的,或者是深度递归很深的症状,可能不应该过多地依赖堆栈.因此,堆栈限制.

Usually, using a ton of stack space is accidental or a symptom of very deep recursion that probably should not be relying so much on the stack. Thus the stack limit.

对表演的影响很小,但确实存在.使用time命令,我发现消除堆栈限制将性能提高了几分之一秒(至少在64位Ubuntu上).

Impact on performace is minor but does exist. Using the time command, I found that eliminating the stack limit increased performance by a few fractions of a second (at least on 64bit Ubuntu).

这篇关于"ulimit -s unlimited"是什么?做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆