Faster Context Switching: BEAM's VM vs. OS Kernel

The BEAM VM handles context switching for its processes entirely in user space, avoiding expensive kernel-level transitions and resulting in context switches that are orders of magnitude faster than those of traditional OS threads.

The top 3

  1. BEAM's User-Space Scheduling vs. OS Kernel: The BEAM VM manages context switching entirely within user space, circumventing the substantial overhead of transitioning to the OS kernel for each switch, which typically involves saving and loading extensive system-level states and memory maps.
  2. Kilobyte Processes: Less State to Switch: Erlang processes are exceptionally lightweight, typically consuming only a few kilobytes of memory. This drastically reduces the amount of state information that needs to be saved and restored during context switches, making them far more efficient than OS threads, which often require megabytes of memory.
  3. Reduction Counting: BEAM's Preemptive Scheduling: The BEAM VM implements its preemptive scheduling using a technique called 'reduction counting.' Each Erlang process is allotted a fixed number of 'reductions' (basic operations), ensuring it yields the CPU after exhausting them, thereby guaranteeing fairness and preventing any single process from monopolizing processing time without relying on the OS kernel's scheduler.

Sources

Open the full topic