Setting up the Proper Size SWAP File in openSUSE

SWAP file usage in our Linux distribution is common to most all other Linux versions and so information gained from other systems running Linux should be usable in openSUSE. Recently after reading an article on how a SWAP file works, I felt that some of that information should be shared with my fellow openSUSE users. You can read the article yourself if you wish in the AUG/SEP 2012 ADMIN magazine called SWAP Tricks by Federico Lucifredi, page 83. Its a great Little Article and worth the cost of the Mag.

So to begin, SWAP disk space is used when we begin to run out of main RAM memory, using it just as if it were regular memory. SWAP space prevents us from running out of memory, which can be a very bad problem should it occur. SWAP space is also used with the computer function called Hibernate, where all of the computer memory state is saved to your hard disk and can be restored at a later date. In this article though, we are concerned about its usage as memory. So, the first thing to decide, is how much should you create, when you install openSUSE? If you are like me, you often just go with the default of 2.00 GB or so. But apparently, there are some basic recommendations for this SWAP file size to be created according to Red Hat recommendations.

  RAM Memory   SWAP File Size

  Up TO   4GB        2GB
 4GB TO  16GB        4GB
16GB TO  64GB        8GB
64GB TO 256GB       16GB

So for most of us its a choice between a 2GB or 4GB SWAP File. Also, computers with large amounts of memory will not want to use SWAP space since it does slow your PC down. So, how can we determine where our SWAP file is located and how much of it is being used?

To determine where it is located, you can use the following Terminal command:

> more /proc/swaps

And this is what I get:

Filename                                Type            Size    Used    Priority
/dev/sda2                               partition       4197372 10456   0

Now that we see my SWAP partition is located on /dev/sda2 Partition, I can use fdisk to make sure it is properly setup as a SWAP partition with the following terminal command:

sudo /sbin/fdisk -l /dev/sda

And this is the result of that command on my system:

Disk /dev/sda: 128.0 GB, 128035676160 bytes
255 heads, 63 sectors/track, 15566 cylinders, total 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000bc91f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     1019897      508925   83  Linux
**/dev/sda2         1019904     9414655     4197376   82  Linux swap / Solaris**
/dev/sda3   *     9414656   250052607   120318976   83  Linux

Having the Partition ID of 82 shows it is a SWAP partition. Now, to determine if it is being used, here is the Terminal Command we can use:

> free -m

             total       used       free     shared    buffers     cached
Mem:         15292      10815       4476          0       1062       8706
-/+ buffers/cache:       1047      14245
**Swap:         4098         10       4088**

The -m option puts the values in millions, and easier to read. OK, well it looks like I have been using some of my 4GB of SWAP space. Even though I have 16GB of RAM memory, I start to use it up with multiple VM’s running so SWAP space will start to be used. Now, did you know there is a way to direct how likely the kernel will use of SWAP space? Consider that having SWAP space keeps you from running out of memory. But, using SWAP space can slow your PC down. The Linux kernel is trying anticipate the use of SWAP space but by default is more likely to use it before its really needed than not. That means there is a way to control its swappiness, as it is called and it can be set from a low of 0 up to 100%, the default. There are three ways to set this value. Th first way is to just edit the file that controls it.

In KDE do Alt-F2: kdesu kwrite /etc/sysctl.conf

OR

In GNOME do Alt-F2: gnomesu gedit /etc/sysctl.conf 

And add the following one line to this existing file:

####
#
# /etc/sysctl.conf is meant for local sysctl settings
#
# sysctl reads settings from the following locations:
#   /boot/sysctl.conf-<kernelversion>
#   /lib/sysctl.d/*.conf
#   /usr/lib/sysctl.conf.d/*.conf
#   /usr/local/lib/sysctl.d/*.conf
#   /etc/sysctl.d/*.conf
#   /run/sysctl.d/*.conf
#   /etc/sysctl.conf
#
# To disable or override a distribution provided file just place a
# file with the same name in /etc/sysctl.d/
#
# See sysctl.conf(5) and sysctl(8) for more information
#
####
kernel.sysrq = 0
net.ipv4.ip_forward = 0
net.ipv4.tcp_syncookies = 1
net.ipv6.conf.all.forwarding = 0
####
#
# /etc/sysctl.conf is meant for local sysctl settings
#
# sysctl reads settings from the following locations:
#   /boot/sysctl.conf-<kernelversion>
#   /lib/sysctl.d/*.conf
#   /usr/lib/sysctl.conf.d/*.conf
#   /usr/local/lib/sysctl.d/*.conf
#   /etc/sysctl.d/*.conf
#   /run/sysctl.d/*.conf
#   /etc/sysctl.conf
#
# To disable or override a distribution provided file just place a
# file with the same name in /etc/sysctl.d/
#
# See sysctl.conf(5) and sysctl(8) for more information
#
####

# net.ipv6.conf.all.disable_ipv6 = 1
# net.ipv6.conf.all.disable_ipv6 = 1

**vm.swappiness=90 **

In this example, we will change the default to 90, down from 100%. Next, we could just change it on the fly with a couple of Terminal commands:

> sudo /sbin/sysctl -w vm.swappiness=90 

OR

> sudo echo 90 > /proc/sys/vm/swappiness

In either case, this setting is good for a single session only and will go back to its default on your next restart or the value you have placed into your /etc/sysctl.conf file. Finally. did you know if you are going to do any memory speed testing, it would be best to turn off the SWAP file operation or anytime you don’t want a SWAP file used that might slow you down. The Terminal command you use would be:

To Turn off the SWAP file you would use:

> sudo /sbin/swapoff -a

To Turn on the SWAP file you would use:

> sudo /sbin/swapon -a

This concludes the SWAP file information I wanted to share with you. As always, if you have any comments or corrections, please let me know what they are.

Thank You for using openSUSE,

Blogs: asroot : Bash : Packet Filter : C.F.U. : GPU’s : fewrup : F.S.M. : H.I. : nVIDIA : LNVHW : N.S.F. : S.A.K.C. : MMCHECK
S.A.S.I. : S.C.L.U. : S.G.T.B. : S.K.I.M. : S.L.A.V.E. : S.L.R.C. : S.T.A.R.T. : S.U.F.F. : SYSEdit : systemd : YaST Power

James,

I did not know that we can control the swappiness. Thanx bro.

[QUOTE=MarcoMeswara;bt647]James,

I did not know that we can control the swappiness. Thanx bro.[/QUOTE]

I am very happy to be at your service MarcoMeswara.

Thank You,

Was there some rule of thumb that you used to determine 90 vs, say 60 ?

And thanks for the interesting read

[QUOTE=hextejas;bt942]Was there some rule of thumb that you used to determine 90 vs, say 60 ?

And thanks for the interesting read[/QUOTE]

Well 90 is just less change to see if it helps without a bigger jump like 60 that might hurt, but the user can try anything they want to use.

Thank You,