Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

On my debian VM machine with 512MB RAM and 348MB Swap, what will happen if i open a 1GB 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
share|improve this question
    
Why don't you give it more Swap? –  guntbert Jun 8 at 20:37
    
HDD space is expensive on my VM –  rubo77 Jun 8 at 20:53
    
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 at 1:17
add comment

2 Answers 2

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.

share|improve this answer
add comment

There is huge problem in Linux is 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 offending memory eater process manually. This is because of huge number of disk operations. To avoid this situation, I personnally usually completely disable swap, so Linux kernel is always responsible and in worst case the oom (out of memory) killer will kill some process. 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 which just constantly allocates some memory in loop. Save this program to text file memeater.c:

#include <stdlib.h>
int main() {
    for (;;) {char* mem=malloc(4096); mem[0]=1;};
}

Than compile it:

$ gcc memeater.c -o memeater

and run:

$ ./memeater

Try 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 10Gb file in editor' or 'run too much graphical file resizes in parallel' and do the conclusion do you need swap or not.

share|improve this answer
    
+1 for suggesting the simple method of testing it out yourself –  rubo77 Jun 9 at 8:03
    
It answers second question "is it good idea to use dynamic swap allocation". –  gena2x Jun 9 at 12:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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