如何增加 ruby​​ 应用程序的堆栈大小.递归应用程序获取:堆栈级别太深(SystemStackError) [英] How to increase stack size for a ruby app. Recursive app getting: Stack level too deep (SystemStackError)

查看:48
本文介绍了如何增加 ruby​​ 应用程序的堆栈大小.递归应用程序获取:堆栈级别太深(SystemStackError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 stackoverflow.com 上发布堆栈溢出问题,多有趣:-)

Posting a stack overflow question on stackoverflow.com, how amusing :-)

我正在运行一些递归 Ruby 代码,我得到:堆栈级别太深(SystemStackError)"

I'm running some recursive Ruby code and I get the: "Stack level too deep (SystemStackError)"

(我很确定代码有效,我没有陷入无限递归的死亡螺旋,但这不是重点)

(I'm quite sure the code works, that I'm not in an infinite recursive death spiral, but that is not the point anyway)

无论如何要更改我的 Ruby 应用程序允许的堆栈深度/大小?

Is there anyway to change the allowed stack depth/size for my Ruby app?

我不太明白这是否是 Ruby 中的限制,因为错误显示堆栈级别",这给我的印象是 Ruby 以某种方式计算堆栈的级别",或者它只是意味着堆栈已满.

I don't quite get it if this is a restriction in Ruby, since the error says "Stack level", which gives me the impression that Ruby somehow counts 'levels' of stack, or if it simply means that the stack is full.

我尝试在 Vista 和 Ubuntu 下运行这个程序,结果相同.在 Ubuntu 下,我尝试使用 'ulimit -s' 将堆栈大小从 8192 更改为 16000,但这没有任何改变.

I have tried running this program under both Vista and Ubuntu with same result. Under Ubuntu i tried changing the stack size with 'ulimit -s' from 8192 to 16000, but that didn't change anything.

感谢您的反馈.
我确实意识到使用递归函数可能不是最可靠的方法.但这也不是重点.我只是想知道是否有办法增加堆栈大小......期间.正如我提到的,我确实尝试在运行 ruby​​ 脚本之前运行 ulimit -s 16000 .. 没有任何改进.. 我用错了吗?

Thanks for the feedback.
I do realize that using a recursive function perhaps isn't the most robust way to go. But that isn't the point either. I simply wonder if there is a way to increase the stack size.. period. And as I mentioned i did try running ulimit -s 16000 before running the ruby script.. with no improvement.. Am I using it wrong?

实际上,我在代码的边缘情况下进行了无限递归.
当您收到 堆栈级别太深" 错误时,截断的 ruby​​ 堆栈跟踪有点误导.
当递归行为涉及多个函数时,您会觉得递归次数比实际少得多.在这个例子中,它可能会在 190 次以上的调用后崩溃,但实际上大约是 15000 次调用

I was in fact having an infinite recursion in an edge case of the code.
The truncated ruby stack trace when you get the "Stack level too deep" error is a bit misleading.
When having a recursive behavior involving several functions, you get the impression that the numbers of recursions is much lower than it actually is. In this example one might thing that it crashes after little bit more than 190 calls, but it is actually around 15000 calls

tst.rb:8:in `p': stack level too deep (SystemStackError)
        from tst.rb:8:in `bar'
        from tst.rb:12:in `bar'
        from tst.rb:19:in `foo'
        from tst.rb:10:in `bar'
        from tst.rb:19:in `foo'
        from tst.rb:10:in `bar'
        from tst.rb:19:in `foo'
        from tst.rb:10:in `bar'
         ... 190 levels...
        from tst.rb:19:in `foo'
        from tst.rb:10:in `bar'
        from tst.rb:19:in `foo'
        from tst.rb:22

-安德烈亚斯

推荐答案

Ruby 使用 C 堆栈,因此您的选择包括使用 ulimit 或使用某些编译器/链接器堆栈大小标志编译 Ruby.尾递归尚未实现,Ruby 当前对递归的支持不是很好.尽管递归很酷且优雅,但您可能需要考虑应对语言的限制并以不同的方式编写代码.

Ruby uses the C stack so your options include using ulimit or compiling Ruby with some compiler/linker stack size flag. Tail recursion is yet to be implemented and Ruby's current support for recursion isn't so great. As cool and elegant recursion is, you might want to consider coping with the language's limitations and writing your code in a different way.

这篇关于如何增加 ruby​​ 应用程序的堆栈大小.递归应用程序获取:堆栈级别太深(SystemStackError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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