How to increase stack size for a ruby app. Recursive app getting: Stack level too deep (SystemStackError)

前端 未结 7 1808
感情败类
感情败类 2020-11-30 02:53

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

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

相关标签:
7条回答
  • 2020-11-30 03:34

    Just had the same issue and it is very easy to fix on Linux or on Mac. As said in the other answers, Ruby uses the system stack setting. You can easily change this on Mac and Linux by setting the stack size. Fox example:

    ulimit -s 20000
    
    0 讨论(0)
  • As of Ruby 1.9.2 you can turn on tail-call optimization with something like:

    RubyVM::InstructionSequence.compile_option = {
      tailcall_optimization: true,
      trace_instruction: false
    }
    
    RubyVM::InstructionSequence.new(<<-EOF).eval
      def me_myself_and_i
        me_myself_and_i
      end
    EOF
    me_myself_and_i # Infinite loop, not stack overflow
    

    That will avoid the SystemStackError error if the recursive call is at the end of the method and only the method. Of course, this example will result in an infinite loop instead. Probably best to debug using shallow recursion (and no optimization) before going after deep recursion.

    0 讨论(0)
  • 2020-11-30 03:43

    Yukihiro Matsumoto writes here

    Ruby uses C stack, so that you need to use ulimit to specify a limit on stack depth.

    0 讨论(0)
  • 2020-11-30 03:45

    Think about what is going on with the code. As other posters have mentioned it is possible to hack the C code of the interpreter. However. the result will be that you are using more RAM and have no guarantee that you won't blow the stack again.

    The really good solution would be to come up with an iterative algorithm for what you are trying to do. Sometimes memoisation can help and sometimes you find you are not using the stuff you are pushing on the stack in which case you can replace recursive calls with mutable state.

    If you are new to this sort of stuff have a look at SICP here for some ideas...

    0 讨论(0)
  • 2020-11-30 03:46

    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.

    0 讨论(0)
  • 2020-11-30 03:49

    If you are sure that you do not have an Infinite recursion situation then your algorythm is pobably not suited for Ruby to execute it in a recirsive manner. Converting an algorythm from recursion to different kind of stack is pretty easy and I suggest you try that. Here is how you can do it.

    def recursive(params)
      if some_conditions(params)
         recursive(update_params(params))
      end
    end
    
    recursive(starting_params)
    

    will transform into

    stack = [starting_params]
    while !stack.empty?
      current_params = stack.delete_at(0)
      if some_conditions(current_params)
        stack << update_params(current_params)
      end
    end
    
    0 讨论(0)
提交回复
热议问题