3

I have a swap partition /dev/sda1. It is 2GB in size and i only want to use it for hibernating. My system has 4GB of RAM. When I am low on RAM the kernel starts swapping and I can't even switch to a tty to kill the application. I only want swap to be used for hibernating, not on OOM condition. So I want OOM killer to kill some applications, not to swap all the time. I have set vm.swappiness=0 but it doesn't help. Any solutions?

3
  • I don't have a solution, but wanted to comment on the vm.swappiness=0 setting. vm.swappiness=0 means that the kernel will wait as long as it dares before starting to swap. It will not stop the swapping completely.
    – Mogget
    Commented Nov 3, 2013 at 17:14
  • I have possible method how to do this - keep the swap umounted and when i need to hibernate quickly mount it. The question is how to make it work with KDE hibernate button.
    – Nazar554
    Commented Nov 3, 2013 at 19:20
  • Turning off swap isn't going to help the underlying problem you will have when you run out of ram. That said, your system going unresponsive when swapping may indicate a hard drive issue as well. Might be worth running some hardware checks.
    – Journeyman Geek
    Commented Nov 3, 2013 at 23:48

3 Answers 3

0

Here are a couple of things you could try, although the solutions are not directly what you asked for.

As I said in the comments, setting vm.swappiness=0 will only make the kernel wait as long as it can before using the swap, not turn it completely off. I have no proof of this but I think that by setting vm.swappiness=0 you are making your problem worse.

The reason I believe that this is the case is that when something happens that force the kernel to start swapping, it will probably have to swap everything really fast, something it is unable to do because of the speed which a hard drive work at. If you let the kernel choose when to swap, it will start swapping data that is used less at an earlier time, and not have to move a lot of data from RAM to swap at once when things get rough. In general this will increase the kernel efficiency.

You could also make a script which will use swapon and swapoff to activate either a swap partition or swap file generated for this specific reason when triggering hibernation. Most window managers can be customized to execute scripts when different part of it is activated like for example the hibernation button. How to do this is dependant on which window manager you use.

A third option is to let the swap be active all the time and instead decrease the amount of RAM that can be used before the OOM manager is activated and start killing processes. This is possible to do, but I have no idea how to do it.

Finally, considering how much RAM cost these days, increasing the amount of RAM in your computer is also a viable option if you have not maxed it out already.

0

I used workaround by adding the following to root's crontab

*/5 * * * * /sbin/swapoff -a  && /sbin/swapon -a

it is not best solution, but keeps swap usage at minimum. It gives me best result since I tried to use other solutions.

Another approach could be to use custom scripts for suspend/wakeup but I not found how to make it working with ubuntu (swapoff on wake, swapon before hibernate)

0

I have the very same issue. "Turning on and off swaps" fits my case. However, swapon/off -a does not work for me because I'm using ZRam. Here is the function for turning on and off the swap devices listed in /etc/fstab:

turn_swaps(){
    # Usage: "turn_swaps on" or "turn_swaps off"
    local state="$1"
    while read -r swp; do
        echo "Turning $swp $state"
        /sbin/swap${state} $swp
done <<< $(grep swap /etc/fstab | awk '{print $1}' | sed 's/^#//')
}

Comment out your swap entries within /etc/fstab, so they aren not mounted on boot. Above function will still be able to enable and disable the devices listed in /etc/fstab.

I didn't try, but the following suggestions might be considered too: https://askubuntu.com/a/299190/371730

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .