<div dir="ltr">To add to the other replies, you can also set a heap size limit and have the VM kill your process if it grows too much:<br><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div>1> erlang:process_flag(max_heap_size, 10000000).</div></div><div><div>#{error_logger => true,kill => true,size => 0}</div></div><div><div>2> Start = fun Start(N) -> N * Start(N - 1) end.</div></div><div><div>#Fun<erl_eval.30.99386804></div></div><div><div>3> Start(1).</div></div><div><div><br></div></div><div><div>=ERROR REPORT==== 9-Oct-2017::11:59:38 ===</div></div><div><div>     Process:          <0.64.0></div></div><div><div>     Context:          maximum heap size reached</div></div><div><div>     Max Heap Size:    10000000</div></div><div><div>     Total Heap Size:  15420698</div></div><div><div>     Kill:             true</div></div><div><div>     Error Logger:     true</div></div><div><div>     GC Info:          [{old_heap_block_size,5157867},</div></div><div><div>                        {heap_block_size,11347307},</div></div><div><div>                        {mbuf_size,0},</div></div><div><div>                        {recent_size,1308970},</div></div><div><div>                        {stack_size,1084478},</div></div><div><div>                        {old_heap_size,0},</div></div><div><div>                        {heap_size,4073388},</div></div><div><div>                        {bin_vheap_size,4},</div></div><div><div>                        {bin_vheap_block_size,46422},</div></div><div><div>                        {bin_old_vheap_size,0},</div></div><div><div>                        {bin_old_vheap_block_size,46422}]</div></div><div><div>** exception exit: killed</div></div><div><div>4></div></div></blockquote><div><br></div>Dominic<br><div class="gmail_extra"><br><div class="gmail_quote">2017-10-07 21:14 GMT+09:00 Arun <span dir="ltr"><<a href="mailto:arunp@utl.in" target="_blank">arunp@utl.in</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    <p>Hi Leandro,</p>
    <p>Am aware about tail recursion in erlang, but here I am talking
      about recursion which is not tail recursive. Here is the code
      snippet;</p>
    <p>start(N) -><br>
          N*start(N-1).</p>
    <p>Here the function is not tail recursive and it is in an infinite
      loop. If run this function my system is getting hang. That why i
      got the doubt why it is making my system to hang and how the
      erlang scheduler will behave in this scenario.?<br>
    </p>
    Regards,<br>
    Arun P.<br>
    <p><br>
    </p>
    <div class="m_-8616746644625387630moz-cite-prefix">On Saturday 07 October 2017 04:07 PM,
      Leandro Ostera wrote:<br>
    </div>
    <blockquote type="cite">
      <div dir="ltr">Hej Arun, hi.
        <div><br>
        </div>
        <div>Depending on your recursion, you may or may not be building
          up a stack that will at some point crash. I am unsure about
          how C compilers typically deal with tail-recursion, but tail
          call optimization is automatically performed in the Erlang VM
          for you.</div>
        <div><br>
        </div>
        <div>From the Learn You Some Erlang book chapter on Recursion (<a href="http://learnyousomeerlang.com/recursion" target="_blank">http://learnyousomeerlang.<wbr>com/recursion</a>):<br>
          <br>
          <p style="background:url("http://learnyousomeerlang.com/static/img/note-background.jpg") no-repeat;margin:0px 0px 1.4em;padding:15px 0px 0px 20px;border:0px;outline:0px;font-size:0.9em;vertical-align:baseline;font-family:verdana,arial,helvetica,sans;line-height:1.4em"><strong style="margin:0px;padding:0px;border:0px;outline:0px;font-size:14.4px;vertical-align:baseline;background:transparent">Note:</strong> tail
            recursion as seen here is not making the memory grow because
            when the virtual machine sees a function calling itself in a
            tail position (the last expression to be evaluated in a
            function), it eliminates the current stack frame. This is
            called tail-call optimisation (TCO) and it is a special case
            of a more general optimisation named <em style="margin:0px;padding:0px;border:0px;outline:0px;font-size:14.4px;vertical-align:baseline;background:transparent">Last
              Call Optimisation</em> (LCO).</p>
          <p style="background:url("http://learnyousomeerlang.com/static/img/note-background.jpg") no-repeat;margin:0px 0px 1.4em;padding:15px 0px 0px 20px;border:0px;outline:0px;font-size:0.9em;vertical-align:baseline;font-family:verdana,arial,helvetica,sans;line-height:1.4em">LCO
            is done whenever the last expression to be evaluated in a
            function body is another function call. When that happens,
            as with TCO, the Erlang VM avoids storing the stack frame.
            As such tail recursion is also possible between multiple
            functions. As an example, the chain of functions <code style="margin:0px;padding:0px;border-width:0px 0px 1px;border-top-style:initial;border-right-style:initial;border-bottom-style:dotted;border-left-style:initial;border-top-color:initial;border-right-color:initial;border-bottom-color:rgb(170,221,153);border-left-color:initial;outline:0px;font-size:1.1em;vertical-align:baseline;background:rgb(238,255,204);color:rgb(85,85,85)">a()
              -> b(). b() -> c(). c() -> a().</code> will
            effectively create an infinite loop that won't go out of
            memory as LCO avoids overflowing the stack. This principle,
            combined with our use of accumulators is what makes tail
            recursion useful.</p>
        </div>
        <div><br>
        </div>
        <div>Hope this helps!</div>
      </div><div><div class="h5">
      <br>
      <div class="gmail_quote">
        <div dir="ltr">On Sat, Oct 7, 2017 at 9:03 AM Arun <<a href="mailto:arunp@utl.in" target="_blank">arunp@utl.in</a>>
          wrote:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
          <br>
          I tried to run normal recursion program from erlang shell, the
          program<br>
          runs infinitely and it is doing addition of numbers. But its
          been<br>
          observed that soon after starting the program entire PC is
          getting hang.<br>
          <br>
          My doubt is that, why my PC is getting hang. ? , if something
          is going<br>
          wrong, the erlang vm should be able to handle it, like in C if
          we run<br>
          some recursion program infinity, it will crash by throwing
          segmentation<br>
          fault error.<br>
          <br>
          What will be happening in the VM if I run a normal recursion
          program in<br>
          erlang infinitely ?<br>
          <br>
          What will be the behavior of erlang scheduler in this scenario
          ?<br>
          <br>
          Can somebody please assist me on this issue.<br>
          <br>
          Thanks in advance<br>
          Arun P.<br>
          ______________________________<wbr>_________________<br>
          erlang-questions mailing list<br>
          <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
          <a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a><br>
        </blockquote>
      </div>
    </div></div></blockquote>
    <br>
  </div>

<br>______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div></div>