Linux – Why can’t the OOM-Killer just kill the process that asks for too much

linuxout of memory

It is explained here: Will Linux start killing my processes without asking me if memory gets short? that the OOM-Killer can be configured via overcommit_memory and that:

  • 2 = no overcommit. Allocations fail if asking too much.
  • 0, 1 = overcommit (heuristically or always). Kill some process(es) based on some heuristics when too much memory is actually accessed.

Now, I may completely misunderstand that, but why isn't there an option (or why isn't it the default) to kill the very process that actually tries to access too much memory it allocated?

Best Answer

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.