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.

I am using Debian sid, hard drive formatted with ext4, running on linux 3.1

I remember on previous linux versions (maybe before 3.0), if I run out of memory, and swap is not enabled, programs will usually crash. This is perfect for my environment: simple web browsing with no critical operations. That is, if I accidentally run across a bad website which uses up too much memory, it just crashes without rendering my terminal unusable.

But in my current setup, the computer hangs with violent I/O throughput in the background. iotop reveals kswapd0 to be the culprit, which means it is due to swapping. After using swapon -s to determine any swaps that were enabled, I used swapoff -a to disable all swaps and swapon -s again to confirm that all swaps were disabled.

Then I tried maximizing my memory usage again. Alas, the behavior I expected didn't happen. Instead, kswapd0 tries over and over to swap out the RAM and fails as there is no swap space. Because it never gives up, my computer is locked in eternal I/O heavy freeze, bad for my disk's health.

Am I doing something wrong in trying to swapoff -a? Why is the behavior different than what it used to be (probably pre-3.0 times)?

share|improve this question
    
That doesn't really make sense. Doing the swapoff -a itself, if there was stuff in the swap, will generate a lot of I/O (and can result in processes getting killed if there is not enough real RAM availabe). Are you sure it's not the swapoff -a that caused the I/O "storm"? –  Mat Nov 15 '11 at 11:40
    
I suppose it is enough to comment the fstab line about swap. Try if the behavior is the same. –  enzotib Nov 15 '11 at 11:48
    
@Mat swapoff -a should disable swap permanently, meaning it should stay disabled after next reboot. I confirmed this. Yet, I/O "storm" still happens during the session after next reboot. For the record, I/O "storm" didn't happen at the moment I did swapoff -a because swap was 0 at that time. –  syockit Nov 15 '11 at 11:48
    
@enzotib I have no swap in my fstab. –  syockit Nov 15 '11 at 11:49
8  
@syockit: swapoff -a is not permanent. –  Mat Nov 15 '11 at 11:51
show 1 more comment

4 Answers 4

A better solution than turning off swap, which will at best cause random processes to be killed when memory runs low, is to set the per process data segment limit for processes that pull stuff off the net. This way a runaway browser will hit the limit and die, rather than cause the whole system to become unusable. Example, from the shell

(ulimit -d 400000; firefox) &

The number after -d is in kilobytes. You should experiment with this on your system to choose the best value for your browsing habits. The parentheses cause a subshell to be created; the ulimit command only affects that shell and its children, isolating its effects from the parent shell.

share|improve this answer
add comment

Disabling swap won't do what you want. You will still get violent I/O throughput, but it will be of clean pages rather than dirty ones.

With no swap, the system will compress the cache of clean (unmodified) pages to near zero, because those are the only pages it can evict from physical memory. It can only evict dirty (modified) pages from memory by writing them to swap, with no swap, it has no way to evict dirty pages.

As you run low on physical memory, each process will have to load its code pages from disk as it evicts the previous process code pages. The result will be violent thrashing and excessive work done by the swap subsystem.

This is a special case of a very important principle: For a well-designed system, you can't make it run better by reducing its choices. Linux is a well-designed system. Removing swap just gives it fewer choices, so it's not surprising that it behaves worse.

share|improve this answer
    
This is only true if you allocate just short of all memory. A run away process usually will be trying to allocate much more, and so it will be killed early, freeing up that memory, as opposed to continuing to swap the system to death trying to accommodate more allocations, hence, disabling swap can be helpful when you only max out your ram usage from a runaway process. –  psusi Nov 15 '11 at 18:39
    
Just short of all memory will pretty much always be allocated. Linux is specifically tuned this way. Do a cat /proc/meminfo on any typical Linux box after a few hours of load. –  David Schwartz Nov 15 '11 at 18:43
    
By allocated I ( and most people ) mean not to the page cache. –  psusi Nov 16 '11 at 3:45
1  
@syockit If you disable paging, you can't run any programs. Paging is the mechanism by which files are read in when mapped into memory. –  David Schwartz Nov 16 '11 at 16:39
1  
@psusi: You are correct if the concern is a runaway process that rapidly blows up in memory consumption. But that's not what the OP is talking about, which is a process that consumes excessive, but not unbounded or massively excessive, memory. As it grows through the large sweet spot (where the cache is squeezed) it will grow more and more slowly as the system thrashes. –  David Schwartz Feb 2 '12 at 23:44
show 9 more comments

To make sure that swap is not used, you'd be better off preventing any swap being added at boot. This can be done, depending on the system, by disabling the swap boot service or just commenting out the swap entry in /etc/fstab.

As far as your hangup is concerned, the stop() function in /etc/init.d/swap might give a clue:

stop()
{
       ebegin "Deactivating swap devices"

       # Try to unmount all tmpfs filesystems not in use, else a deadlock may
       # occure. As $RC_SVCDIR may also be tmpfs we cd to it to lock it
       cd "$RC_SVCDIR"
       umount -a -t tmpfs 2>/dev/null

       case "$RC_UNAME" in
               NetBSD|OpenBSD) swapctl -U -t noblk >/dev/null;;
               *)              swapoff -a >/dev/null;;
       esac
       eend 0
}

Notice the part about deadlock. You can try doing umount -a -t tmpfs yourself before turning swap off.


Edit:

Probably, you might also achieve your goal by modifying sysctl settings (see this question).

share|improve this answer
    
I don't have swap in init.d, nor do I have it on fstab, but I do have /etc/init.d/mountoverflowtmp that mounts tmpfs for emergency log writes. Does the swap daemon use tmpfs too? –  syockit Nov 15 '11 at 11:58
    
You might have it enabled elsewhere - do grep -RF swap /etc/ if you wish to find it. But to disable a service, you'd use a command like service (IIRC; I don't use Debian myself). –  rozcietrzewiacz Nov 15 '11 at 12:02
1  
Swap itself does not use tmpfs, because tmpfs is an in-memory (RAM) filesystem. But other services/programs that use tmpfs might rely on swap in a special manner. I don't really know, but it might have something to do with caching or a special way in which tmpfs driver claims access to swap space. –  rozcietrzewiacz Nov 15 '11 at 12:04
    
There's something about how Linux handles virtual memory that I don't understand. I've disabled swap in most ways possible: via swapoff, and via vm.swappiness=0. Yet kswapd0 still runs! I wonder if this is a regression from the 2.4 days… –  syockit Nov 15 '11 at 15:36
2  
@syockit It's expected behavior. The system is still swapping clean pages (pages that contain copies of file data). It requires no swap space to swap clean pages, since they can be read back from sources other than swap. –  David Schwartz Nov 15 '11 at 16:58
add comment

It is better to comment out swap partition entry in /etc/fstab than running swapoff -a after each boot.

I have the same issue with kswapd0 on my hardware.

Tuning vm.swappiness system parameter does not help for me.

sysctl -w vm.swappiness=0

I googled and read a lot of posts, mailing lists, and now I think that this is kernel bug.

When there is no active swap partition and free memory becomes less then some threshold (about 300MB in my case) the system becomes unresponsive due to kswapd0 madness.

Probably it is reproduced with special configuration and conditions.

For somebody it is solved by system re-installation with re-partitioning for others by building custom kernel with kswapd0 disabled.

share|improve this answer
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.