Why doesn't factorial overflow the stack in Erlang?

-module(demo).
-export([factorial/1]).

factorial(0) -> 1;
factorial(N) -> 
    N * factorial(N-1).

Factorial is not tail recursive, but why doesn't it overflow the stack? I can get a factorial of 100,000 without, but it takes some time to calculate.

+4
source share
1 answer

The "Erlang stack stack" is not stored on the system-defined stack for the process (usually several megabytes), but on the heap. As far as I know, it will grow indefinitely until the system refuses to provide the VM with more memory.

Size includes 233 words for the heap area ( which includes the stack ). The garbage collector increases the heap as needed.

() . , .

Erlang VM , Activity Monitor, OSX top UNIX- , , , , (, "" ) ( ).

+8

Source: https://habr.com/ru/post/1651357/


All Articles