12

Fibers are a relatively new concept to me. I'm aware that each fiber's stack size is limited to 4kB and I keep reading that I should "beware" of this. What exactly are the real world consequences of this limit?

Edit:

It seems that this 4kB limitation isn't such a hindrance after all and it takes a good number of local variables (4,045) within the fiber itself to cause a SystemStackError to be raised.

count = 0
loop do
  count += 1
  puts count
  varlist = String.new
  count.times do |i|
    varlist += "a#{i} = 1\n"
  end
  s = "fiber = Fiber.new do \n #{varlist} \n end \n fiber.resume"
  eval(s)
end

Not the most elegant code but it does seem to demonstrate the limitations of a fiber's stack. It seems as though it's only return values, local variables (all of which contain a reference to an object on the heap) and method calls get put on the stack. I haven't tested whether the local variables etc in methods that are called from a fiber are part of the fiber's stack.

Edit 2:

Modified the above code. It does appear that variables etc in called methods become part of the fiber's stack. If this is the case, then the call depth (even without recursion) could be more of an issue as methods themselves are likely to require more space on the stack than variables (which seem to be transparent references to objects on the heap).

The following code fails on the 4,031st iteration and indicates variables in called methods become part of the fiber's stack:

count = 0
loop do
  count += 1
  puts count
  varlist = String.new
  count.times do |i|
    varlist += "a#{i} = 1\n"
  end
  m = "def meth\n #{varlist} \n end"
  eval(m)
  fiber = Fiber.new do
    meth
  end
  fiber.resume
end

Edit 3:

Just tried running the initial code example on Rubinius 2.0. Its fibers don't seem to have a 4kB stack limit, although beyond about 3,500th iteration it becomes increasingly and noticeably slow, and at around the 5,000th iteration it's averaging about one iteration a second. I don't know if there is a limit with RBX because I quit execution at just over 5,100 iterations. RBX is also using several times more memory than MRI 1.9.3.

JRuby 1.7 also doesn't seem to have a 4kB stack size for fibers and if fibers have a max stack size it's unknown to me. I completed 5,000 iterations of the first code example without problems, although as can be expected, the JVM chewed through a few hundred MB of RAM.

Matty
  • 33,203
  • 13
  • 65
  • 93
  • 2
    maybe you should avoid deep recursive flow? – Ron Klein Nov 30 '12 at 03:25
  • 1
    Could you post the references that are advising caution? – Peter Brown Nov 30 '12 at 03:34
  • @Beerlington Here are couple I could find (with one more that I can't stumble across): https://github.com/mperham/rack-fiber_pool/issues/17 and http://lists.basho.com/pipermail/riak-users_lists.basho.com/2012-March/007877.html – Matty Nov 30 '12 at 04:47

2 Answers2

3

As Anton mentioned in his answer, you memory intensive code within a fiber. Examples of stuff that could (potentially) eat up a lot of memory:

  • Large strings (ie: a string containing a decent sized HTTP response)
  • Recursive functions (Stack Level Too Deep!)
  • Streams or stream like objects: be VERY careful about stream buffers; if they get near or exceed 4k you'll start seeing some very strange behavior
Peter Brown
  • 50,956
  • 18
  • 113
  • 146
Damien Wilson
  • 4,540
  • 3
  • 21
  • 27
  • 1
    Are you sure that strings live on the stack? I thought they lived on the heap; only references to them living on the stack. – Wayne Conrad Nov 30 '12 at 04:01
  • Wayne: It's possible I've reached an incorrect conclusion, but I have seen stack level errors when dealing with bloated fibers. – Damien Wilson Nov 30 '12 at 04:03
  • As far as I know, static variables are declared in the stack and dynamic variables (from return values for example),methods and attributes are defined in the heap. So if a string is defined inside a method, it is in the stack, if it is a class attribute for example, it is on the heap. – Anton Garcia Dosil Nov 30 '12 at 04:28
  • 3
    It appears as though strings and other objects are all allocated on the heap. An exception is thrown when a fiber contains 4,045 variables, which indicates the fiber has some overhead and that the variable itself (not the object it refers to) consumes one byte. – Matty Nov 30 '12 at 05:07
  • This is all very interesting. Does anybody know where these behaviors are documented? I assume we're all referring to MRI, correct? – Damien Wilson Nov 30 '12 at 06:15
  • 1
    @DamienWilson To the best of my knowledge it's not officially documented. Everything I've found out so far (see edits to question) has been as a result of writing code and trying to break things. I suppose the only true documentation is the C source code. I'm running MRI 1.9.3. – Matty Nov 30 '12 at 07:13
  • 3
    "Ruby Under a Microscope" documents how strings are stored in MRI 1.9. "When Ruby saves a string value on the YARV stack--or any object, for the matter--it actually only places the reference to the thing on the stack. The actual string structure is saved in the "heap" instead. – Wayne Conrad Dec 01 '12 at 01:41
2

The consequences of that are that you must pay more attention to the memory of your Fiber code, because you might have memory leaking.

Some recursive functions might give you problems