[erlang-questions] System getting hang after running normal recursion program
Dominic Morneau
dmorneau@REDACTED
Mon Oct 9 05:12:38 CEST 2017
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:
1> erlang:process_flag(max_heap_size, 10000000).
#{error_logger => true,kill => true,size => 0}
2> Start = fun Start(N) -> N * Start(N - 1) end.
#Fun<erl_eval.30.99386804>
3> Start(1).
=ERROR REPORT==== 9-Oct-2017::11:59:38 ===
Process: <0.64.0>
Context: maximum heap size reached
Max Heap Size: 10000000
Total Heap Size: 15420698
Kill: true
Error Logger: true
GC Info: [{old_heap_block_size,5157867},
{heap_block_size,11347307},
{mbuf_size,0},
{recent_size,1308970},
{stack_size,1084478},
{old_heap_size,0},
{heap_size,4073388},
{bin_vheap_size,4},
{bin_vheap_block_size,46422},
{bin_old_vheap_size,0},
{bin_old_vheap_block_size,46422}]
** exception exit: killed
4>
Dominic
2017-10-07 21:14 GMT+09:00 Arun <arunp@REDACTED>:
> Hi Leandro,
>
> Am aware about tail recursion in erlang, but here I am talking about
> recursion which is not tail recursive. Here is the code snippet;
>
> start(N) ->
> N*start(N-1).
>
> 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.?
> Regards,
> Arun P.
>
>
> On Saturday 07 October 2017 04:07 PM, Leandro Ostera wrote:
>
> Hej Arun, hi.
>
> 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.
>
> From the Learn You Some Erlang book chapter on Recursion (
> http://learnyousomeerlang.com/recursion):
>
> *Note:* 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 *Last Call
> Optimisation* (LCO).
>
> 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 a() ->
> b(). b() -> c(). c() -> a(). 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.
>
> Hope this helps!
>
> On Sat, Oct 7, 2017 at 9:03 AM Arun <arunp@REDACTED> wrote:
>
>> Hi all,
>>
>> I tried to run normal recursion program from erlang shell, the program
>> runs infinitely and it is doing addition of numbers. But its been
>> observed that soon after starting the program entire PC is getting hang.
>>
>> My doubt is that, why my PC is getting hang. ? , if something is going
>> wrong, the erlang vm should be able to handle it, like in C if we run
>> some recursion program infinity, it will crash by throwing segmentation
>> fault error.
>>
>> What will be happening in the VM if I run a normal recursion program in
>> erlang infinitely ?
>>
>> What will be the behavior of erlang scheduler in this scenario ?
>>
>> Can somebody please assist me on this issue.
>>
>> Thanks in advance
>> Arun P.
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171009/ca775327/attachment.htm>
More information about the erlang-questions
mailing list