[erlang-questions] System getting hang after running normal recursion program

zxq9 zxq9@REDACTED
Sun Oct 8 03:49:28 CEST 2017

On 2017年10月07日 土曜日 17:44:00 Arun wrote:
> 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.?

Infinite depth recursion will make your system run out of memory, not CPU. The call stack is just growing and growing and growing...

This has nothing to do with the schedulers at all -- they will behave the way they always would, but will be starved for things to do because your system will the thrashing (swapping in and out of virtual memory on disk in the common case).

The reason this slows the entire system down is because the WHOLE SYSTEM is now starved for memory and is thrashing, including your OS and anything else running. Eventually the size of the Erlang runtime will grow large enough that the system's OOM killer will terminate it and things will return to normal.

If you want to see this effect firsthand but much more quickly, turn off swap.

That said, deep recursion is occasionally the correct approach to take, particularly in problems where the natural solution is iterative and deep, and the depth is known to be finite (the shape of some trees, like filesystems, and certain cases of document or string processing).


More information about the erlang-questions mailing list