Why can't the OOM-Killer just kill the process that asks for too much?

Consider this scenario:

  • You have 4GB of memory free.
  • A faulty process allocates 3.999GB.
  • You open a task manager to kill the runaway process. The task manager allocates 0.002GB.

If the process that got killed was the last process to request memory, your task manager would get killed.

Or:

  • You have 4GB of memory free.
  • A faulty process allocates 3.999GB.
  • You open a task manager to kill the runaway process. The X server allocates 0.002GB to handle the task manager's window.

Now your X server gets killed. It didn't cause the problem; it was just "in the wrong place at the wrong time". It happened to be the first process to allocate more memory when there was none left, but it wasn't the process that used all the memory to start with.