0

I have a Ubuntu 14.04 installation on my 32gb USB drive. I recently read that having a Swap causes a lot of reads and writes to the drive making the OS slow and reducing the lifespan of the drive. When I checked my RAM usage yesterday using free, out of 4GB about 3GB was being used, and out of 1.5GB of Swap, 900MB was being used. Is it safe for me to just delete the swap partition?

1

Read below first!

Hi Ian, yes you could delete Swap with a live stick, but I don't recommend this. This may cause issues, depending on what OS you are using. There is a much nicer way to do that: Fire up a terminal (ctrl + alt + t), then type

sudo swapoff /dev/hdb2

replace hdb2 with your swap partition directory. You can get the directory by typing

sudo fdisk -l

So now your swap should be disabled. Then just remove the swap entry from /etc/fstab. You can do this by typing

sudo nano /etc/fstab

Then hit enter and remove the line which extends the swap informations.

REBOOT!

After that you can easily delete the swap partition with gparted or a partition application of your choice. But I recommend you to first let it be there.

STOP, BEFORE YOU DO ANYTHING MENTIONED ABOVE, READ THIS: You are right, a swap partition produce much of traffic, but this might not make the OS slower. Another possibility instead of deleting the swap is to customize it: Fire up a terminal and run this:

sudo nano /etc/sysctl.conf

Then add this line:

vm.swappiness=10

This customizes the swap, when it should be used. As lower the swapiness is, as less swap will be used. 10 is already extreme less. So swap will only be used in the worst case when there is absolutely no more RAM available.

That's it!

Sources:

0

Not really, no.

However, instead of disabling swappiness, you can run a script such as Zram to use compressed ram memory as swapspace instead of using a physical partition so that your system doesn't crash.

STEP ONE:

Execute the following command in an open terminal:

sudo nano /etc/init.d/zram

And copy/paste the following into the file:

### BEGIN INIT INFO
# Provides: zram
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     S
# Default-Stop:      0 1 6
# Short-Description: Use compressed RAM as in-memory swap
# Description:       Use compressed RAM as in-memory swap
### END INIT INFO

# Author: Antonio Galea <antonio.galea@gmail.com>
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization

FRACTION=75

MEMORY=`perl -ne'/^MemTotal:\s+(\d+)/ && print $1*1024;' < /proc/meminfo`
CPUS=`grep -c processor /proc/cpuinfo`
SIZE=$(( MEMORY * FRACTION / 100 / CPUS ))

case "$1" in
  "start")
    param=`modinfo zram|grep num_devices|cut -f2 -d:|tr -d ' '`
    modprobe zram $param=$CPUS
    for n in `seq $CPUS`; do
      i=$((n - 1))
      echo $SIZE > /sys/block/zram$i/disksize
      mkswap /dev/zram$i
      swapon /dev/zram$i -p 10
    done
    ;;
  "stop")
    for n in `seq $CPUS`; do
      i=$((n - 1))
      swapoff /dev/zram$i && echo "disabled disk $n of $CPUS" &
    done
    wait
    sleep .5
    modprobe -r zram
    ;;
  *)
    echo "Usage: `basename $0` (start | stop)"
    exit 1
    ;;
esac

Then, press CTRL + o to save the file and press CTRL + x to exit the file.


STEP TWO:

First, disable disk swappines:

sudo swapoff -a

Then, execute the following three commands in an open terminal to make the script executable, to start zram, and to make zram automatically start at boot:

sudo chmod +x /etc/init.d/zram
sudo /etc/init.d/zram start
sudo update-rc.d zram defaults

STEP THREE:

Once you have enabled zram swap, you must edit /etc/fstab so that disk swappiness is not enabled at boot:

sudo nano /etc/fstab

Locate the line that begins with:

# swap was on . . .

And place a # at the beginning of the following line to change it from:

UUID= . . . 

to this instead

#UUID= . . .

example:

# swap was on /dev/sdb5 during installation
#UUID=abcd65432-d109-e8765-f43-210987ghi654 none            swap    sw              0       0

or you can delete the line instead.

Finally, press CTRL + o to save the file and press CTRL + x to exit the file.

No need to reboot. Also, you can verify zram is in use by executing the following command:

sudo swapon -s

Sources:

https://wiki.debian.org/ZRam

https://code.google.com/p/compcache/

http://en.wikipedia.org/wiki/Zram


To explain:

While you may have 4GB of ram and may not be "using" a lot of ram for what you are running, linux uses up the remaining ram as cached ram to actually reduce disk activity by caching used applications to ram rather than having to page them from disk next time you open the application. When cached ram fills up ram or when an application needs to use the ram the cached data occupies, the system swaps out some of the cached ram to the swap partition to free up ram for applications in use and to cache more recently used applications in ram.

Also, some form of swappiness is necessary in order to hibernate and/or suspend.

Because of swappiness, your system is able to better utilize your available ram, reduce overall disk activity, and dramatically increase system responsiveness when under a heavy load all while preventing system crashes from maxed out ram.

  • will this method reduce the read/writes on my USB drive? – Ian May 1 '15 at 4:14
  • @Ian Yes it will because the system will no longer use the USB drive for swappiness. Instead, swap is compressed and stored in RAM. Also, it can actually make the system a lot faster as well because RAM is considerably faster than USB flash. – mchid May 3 '15 at 2:30
  • @Ian Also, compared to using no swapiness, read/writes will be reduced as cached applications will subsequently be accessed from RAM rather than from the USB device. – mchid May 3 '15 at 2:36

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.