Posts Tagged swap

Adding more swap space on the fly

Posted by on Tuesday, 7 April, 2009

Sometimes in the course of a system’s existence you find that the swap partition you set up at install-time just isn’t enough anymore. Maybe you’re upgrading your system to RedHat 7.1 from a version of RedHat that used less swap in relation to physical RAM. Perhaps you’re running Oracle. Or maybe you’re adding more memory and would like to increase swap space accordingly.

Our machine srv-2 is swapping like mad and we just can’t take it down right now to add more RAM. So to keep the machine from running out of memory entirely and freezing, we’ll add 128 MB more swap space by creating a swap file.

First we check out the memory usage:

[root@srv-2 /root]# free -m
total       used       free     shared    buffers     cached
Mem:           251        242          8         22         11         32
-/+ buffers/cache:        198         52
Swap:          133        133          0

Make sure we have 128 MB laying around somewhere:

[root@srv-2 /root]# df
Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/hda9               132207     33429     91952  27% /
/dev/hda1                15522      2537     12184  17% /boot
/dev/hda6              6143236    739000   5092176  13% /opt
/dev/hda7              1035660    836204    146848  85% /usr
/dev/hda5              2071384    344048   1622112  17% /usr/local
/dev/hda8               303344     14439    273244   5% /var

OK, we’re going to make a swap file in /opt by using dd to create a file 128 MB in size.

[root@srv-2 /opt]# dd if=/dev/zero of=swapfile bs=1024 count=132207
132207+0 records in
132207+0 records out
[root@srv-2 /opt]# ls -l
total 132364
drwxr-xr-x   20 usr-3 users        4096 May 22 10:46 usr-3
drwxr-xr-x    2 root     root        16384 Feb 21 07:04 lost+found
-rw-r--r--    1 root     root     135379968 May 29 11:52 swapfile

Hey, I know, let’s not make it world-readable…

[root@srv-2 /opt]# chmod 600 swapfile
[root@srv-2 /opt]# ls -l
total 132364
drwxr-xr-x   20 usr-3 users        4096 May 22 10:46 usr-3
drwxr-xr-x    2 root     root        16384 Feb 21 07:04 lost+found
-rw-------    1 root     root     135379968 May 29 11:52 swapfile

Now we set up the swap area and enable it.

[root@srv-2 /opt]# mkswap swapfile
Setting up swapspace version 1, size = 135372800 bytes
[root@srv-2 /opt]# swapon swapfile

And viola! Twice as much swap as before.

[root@srv-2 /opt]# free
total       used       free     shared    buffers     cached
Mem:        257632     254632       3000       2512      36172      15096
-/+ buffers/cache:     203364      54268
Swap:       268708     136512     132196

You can edit /etc/fstab to enable your swap file automatically at boot time.
By adding an entry like this:

/opt/swapfile           swap                    swap    defaults        0 0

Sure, swapping’s ugly, slow and will grind your hard drives to dust. But even modern systems which have been tuned for performance require a generous oodle of swap space. Last-Modified: 2008-03-25 09:57:45


Tweak your swappiness

Posted by on Tuesday, 7 April, 2009

In the world of operating system design, a lot of critical decisions are made by the developers concerning how the system uses the limited resources available to it.

Part of the design pertains to memory, and making use thereof. Most modern operating systems go by the theory that “free” can be equated with wasted when it comes to RAM utilisation.

For this reason Linux attempts to use your unused memory for one of two things, disk cache or virtual memory (aka swap).

A disk cache holds files in memory as they are read off the disk (usually also handling tasks like pre-fetching file contents before they’re requested).

On the other hand, virtual memory is (at least perceived to be) used primarily when there is not enough memory available.

Linux also makes use of virtual memory by proactively swapping out inactive tasks once a threshold of inactivity has been reached.

On the face of things this seems a very valid thing to do, after all who wants an application that only does anything once a day slurping up RAM for the rest of the time. Why not swap it out to virtual memory until it needs to do something?

The problem for interactivity is that Linux is not very granular or intelligent about how it manages the inactive tasks, often switching out tasks like instant messenger applications.

Often the whole point of leaving an application open all the time is to allow instant access to it, and swapping it out to disk effectively kills that benefit by creating a delay while you wait for the pages to be swapped back into memory.

Frequently swapping back an inactive application triggers the threshold for swapping out other active applications like the web browser.

The end result could pan out something like this; you’re reading a web page, when you get a message in your IM application, it swaps into ram to notify you, you pause to look at the message long enough for the kernel to decide your browser is inactive, you continue reading your web page, “activating” and swapping your browser back in and then decide to reply to the message, swapping your “inactive” IM program back in.

That’s a lot of disk activity, considering that you may well have enough RAM to contain both applications at the same time.

I’ve chosen a ridiculous worst case scenario to indicate something that might happen if you live in a lake of semi-frozen treacle and it takes 5 minutes for you to change from one application to another, but it’s still quite possible to end up in scenarios like this in the real world without needing to be submerged in viscous goo.

Thankfully the developers of Linux made provision for end users to easily change this behaviour using the /proc virtual filesystem. Here’s how to do it…

echo 0 > /proc/sys/vm/swappiness

The usual default value of swappiness is 60, valid values are from 0 (no pro-active swapping) to 100 (high tendency to swap out inactive tasks).

My opinion is that swapping out tasks before virtual memory is required is generally unhelpful on desktop computers unless you have VERY fast virtual memory so I set swappiness to 0, but using the example and information above you should be able to figure out how to tune swappiness to your liking.
Last-Modified: 2007-03-07 19:38:50