Linux – What Happens When Out of Swap Memory?

memoryout of memoryswap

On my Debian VM machine with 512 MB RAM and 348 MB swap, what will happen if I open a 1 GB file in an editor and get out of memory?

Will it crash the system? Or if not, how will Linux handle this?

Wouldn't it be wise to install Swapspace so if needed there will be created enough swap automatically and dynamically?

sudo apt-get install swapspace

Best Answer

It depends on the settings you're running with, in particular memory overcommit (/proc/sys/vm/overcommit_memory; see man 5 proc for details).

If memory overcommit is disabled, the editor's (and possibly other programs attempting at the same time) attempt to allocate memory will fail. They'll get a failure result from the system call. Its up to each program to handle this, though an unfortunately common result is for the program to crash. The editor may also, for example, just refuse to open the file.

If memory overcommit is enabled, then the system call requesting memory may well succeed. In that case, when the memory is actually accessed, the kernel will notice its out of memory, and kill a process to reclaim memory. That process may or may not be the editor. The choice is governed by the oom_score (the result of several kernel heuristics) and oom_score_adj (configured) of each process on the system. Those are also in that proc(5) manpage.

Related Question