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
  • Why don't you give it more Swap?
    – guntbert
    Jun 8 '14 at 20:37
  • HDD space is expensive on my VM
    – rubo77
    Jun 8 '14 at 20:53
  • 1
    Note some editors just mem-map the file and do not load it all up front, this fixes the issue entirely as they use the source file itself as 'swap'.
    – Vality
    Jun 9 '14 at 1:17

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.


There is a huge problem in Linux in this case if you approach to the out-of-memory condition - you will notice that whole your system becomes totally unresponsive because it starts a lot of swapping. Even your mouse cursor may become so 'slow' that you can't start a terminal and kill an offending memory eater process manually. This is because of huge number of disk operations.

To avoid this situation, I personally usually completely disable swap, so the Linux kernel is always responsive and in worst case the out of memory (OOM) killer will kill some process. The logic of which process is killed by OOM depends on the kernel version.

So answer is no - don't enable dynamic swap allocation. You will face machine hangs.

It's easy to try it out with program that just constantly allocates some memory in a loop. Save this program to a text file, memeater.c:

#include <stdlib.h>

int main() {
    for (;;) {char* mem=malloc(4096); mem[0]=1;};

Then compile it:

$ gcc memeater.c -o memeater

and run:

$ ./memeater

Try it with swap, without swap, and with your dynamic swap allocation.

Also, keep in mind that in most cases this OOM condition happens because a bug in software (memory leak) or you did something wrong like 'load this 10 GB file in editor' or 'run too many graphical file resizes in parallel' and do the conclusion: Do you need swap or not?

  • 1
    +1 for suggesting the simple method of testing it out yourself
    – rubo77
    Jun 9 '14 at 8:03
  • 1
    It answers second question "is it good idea to use dynamic swap allocation".
    – gena2x
    Jun 9 '14 at 12:33
  • @gena2x Yes, it is a very good idea, because it makes your system more responsive in overloaded situations, instead of killing your tasks each after the other. -1. Although the best if you have a big, fixed swap partition, but it has another cause (reduced swap fragmentation).
    – peterh
    Aug 14 '14 at 15:52
  • If you have big fixed swap you system will become unresponsive because it would try to swap-out active tasks to disk and them swap them back forever.
    – gena2x
    Aug 5 '16 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.