Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang VM killed when creating millions of processes

Tags:

process

erlang

So after Joe Armstrongs' claims that erlang processes are cheap and vm can handle millions of them. I decided to test it on my machine:

process_galore(N)->
    io:format("process limit: ~p~n", [erlang:system_info(process_limit)]),
    statistics(runtime),
    statistics(wall_clock),
    L = for(0, N, fun()-> spawn(fun() -> wait() end) end),
    {_, Rt} = statistics(runtime),
    {_, Wt} = statistics(wall_clock),
    lists:foreach(fun(Pid)-> Pid ! die end, L),
    io:format("Processes created: ~p~n
          Run time ms: ~p~n
          Wall time ms: ~p~n
          Average run time: ~p microseconds!~n", [N, Rt, Wt, (Rt/N)*1000]).

wait()->
    receive die ->
         done
    end.

for(N, N, _)->
    [];
for(I, N, Fun) when I < N ->
    [Fun()|for(I+1, N, Fun)].

Results are impressive for million processes - I get aprox 6.6 micro! seconds average spawn time. But when starting 3m processes, OS shell prints "Killed" with erlang runtime gone. I run erl with +P 5000000 flag, system is: arch linux with quadcore i7 and 8GB ram.

like image 446
Sharas Avatar asked Aug 06 '16 17:08

Sharas


People also ask

How do I kill an Erlang VM?

Of course, the Erlang VM is in the end just a process in your OS. And, if your OS is UNIX-like, you can kill processes by sending signals to them. Different signals have different impacts in an Erlang VM. The usual signals work as expected: kill -SIGTERM will behave like init:stop (). and kill -SIGKILL will behave like erlang:halt (137).

What is an Erlang process?

These processes in Erlang are different than the processes and threads most people are familiar with. Erlang processes are lightweight, operate in (memory) isolation from other processes, and are scheduled by Erlang’s Virtual Machine (VM).

Does Erlang’s Let It crash?

Regardless of Erlang’s legendary Let It Crash philosophy (a.k.a. The Zen of Erlang), there is one process in an Erlang VM that’s not allowed to crash: the application controller. So, if you kill it…

How does concurrency work in Erlang?

The granularity of concurrency in Erlang is a process. A process is an activity/task that runs concurrently with and is independent from the other processes. These processes in Erlang are different than the processes and threads most people are familiar with.


1 Answers

Erlang processes are cheap, but they're not free. Erlang processes spawned by spawn use 338 words of memory, which is 2704 bytes on a 64 bit system. Spawning 3 million processes will use at least 8112 MB of RAM, not counting the overhead of creating the linked list of pids and the anonymous function created for each process (I'm not sure if they're shared if they're created like you're creating.) You'll probably need 10-12GB of free RAM to spawn and keep alive 3 million (almost) empty processes.

As I pointed out in the comments (and you later verified), the "Killed" message was printed by the Linux Kernel when it killed the Erlang VM, most likely for using up too much RAM. More information here.

like image 117
Dogbert Avatar answered Oct 11 '22 19:10

Dogbert