Many processes

The small cost of a process in Elixir is the reason to start many of them. A program that handles a request per process, a file per process or a row per process pays a few kilobytes per unit, not a thread of the operating system. This lesson starts a hundred thousand processes, measures what one of them costs, and shows the limit of the VM.

A hundred thousand pids

The program starts a process per number, each one sends one message back, and the parent counts the replies. The list of pids is not kept, because the message is enough:

many.exs

parent = self() for _ <- 1..100_000 do spawn(fn -> send(parent, :done) end) end for _ <- 1..100_000 do receive do :done -> :ok end end IO.puts("100000 processes answered")

elixir many.exs

100000 processes answered

Every child ends as soon as it has sent its message, so the program does not keep a hundred thousand processes alive; it starts and finishes them in a stream. The count of the loop is the count of the replies, which is how a program waits for work without collecting the pids first.

What one process costs

A process that does nothing but sleep still owns a heap, a stack and a mailbox, and Process.info/2 reports the memory in bytes. The exact number depends on the build and the version, so the program tests an order of magnitude rather than printing a value that changes:

memory.exs

pid = spawn(fn -> Process.sleep(:infinity) end) {:memory, memory} = Process.info(pid, :memory) IO.puts(memory < 10_000)

elixir memory.exs

true

Ten thousand idle processes therefore cost tens of megabytes, not gigabytes, and the number is what makes a process per connection normal on this VM. The processes lesson used the same call to show Process.sleep/1; the value here is the size, which a program reads when it needs a budget.

The limit of the VM

The VM has a hard limit on the number of processes, and a program can read it. The default is high enough that the limit is rarely the first thing a program hits; memory and the work per process come first:

limit.exs

IO.puts(:erlang.system_info(:process_limit)) IO.puts(:erlang.system_info(:process_count) > 0)

elixir limit.exs

1048576
true

:process_count includes the processes of the runtime itself, so the exact number is not stable and the program only asks whether it is positive. The limit is raised at startup with +P when a system really needs more; a program that reaches it gets :system_limit from spawn/1, which is a failure a supervisor cannot fix by restarting faster.

The schedulers

The processes above did not run one at a time. The VM runs one scheduler per core by default, and each scheduler takes a process from a run queue and runs it for a while. The count is a property of the machine, so the page prints it and says that a reader sees the count of their own machine:

schedulers.exs

IO.puts(System.schedulers_online()) IO.puts(System.schedulers_online() >= 1)

elixir schedulers.exs

4
true

The first line is the count of this machine and the second is the stable part. A scheduler runs Elixir code until the process yields or its reduction budget runs out, which is why a tight loop cannot starve the other processes forever. A long NIF does not yield, and that is the case the lesson on a GenServer that is one process warns about.

The mistake: a child that outlives the script

A script ends when its main process ends, and the VM takes every other process with it. A child started for later work therefore loses its chance to print, and the program looks as if the spawn never happened:

early.exs

spawn(fn -> Process.sleep(50) IO.puts("the child printed") end) IO.puts("the script is done")

elixir early.exs

the script is done

The fix is to wait for the child, with a message, a monitor or Task.await/1. The VM ends the whole node when the main process finishes, and this is a feature: a script that forgets a background process does not leave it running on the machine.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Why is a process cheaper than an operating-system thread?

    Answer

    It has a small heap and stack of its own and the VM schedules it

    The BEAM schedules lightweight processes itself, each with a small heap, stack and mailbox.

  2. What does a script do with the processes it started when the main process ends?

    Answer

    It ends them with the node

    The VM stops when the main process ends, and the other processes go with it.

  3. What does :erlang.system_info(:process_limit) report?

    Answer

    The maximum number of processes the VM allows

    It is the configured limit, not the current count; :process_count is the current count.

  4. What does System.schedulers_online/0 report?

    Answer

    The number of cores the VM schedules on

    It is the number of schedulers currently available, usually one per core.

  5. What does this program print? parent = self() for n <- 1..1000 do spawn(fn -> send(parent, n) end) end sum = for _ <- 1..1000 do receive do n -> n end end |> Enum.sum() IO.puts(sum)

    Answer

    500500

    The sum of 1 to 1000 is 500500, and every child sends its number once.

  6. What does this program print? spawn(fn -> Process.sleep(50); IO.puts("late") end) IO.puts("main")

    Answer

    main

    The script ends before the child wakes, and the VM stops the child with it.

  7. What does this program print? pids = for _ <- 1..100, do: spawn(fn -> Process.sleep(:infinity) end) IO.puts(length(pids)) IO.puts(Enum.count(pids, &Process.alive?/1))

    Answer

    100 then 100

    Every sleeping process is still alive when the count is taken.

  8. What happens when the VM reaches its process limit?

    Answer

    spawn fails with :system_limit

    The limit is hard; spawn answers an error rather than silently waiting.

  9. Why can a hundred thousand processes run on one machine?

    Answer

    Because each one costs a few kilobytes

    The small per-process footprint is what makes the count practical.

  10. A process runs a long NIF that never yields. What does the scheduler do?

    Answer

    It cannot preempt the NIF, so that scheduler waits

    The scheduler preempts Elixir code through reductions; a long NIF does not yield.

Article author: Arthur Isaev