TUTORIAL: All about Linux swap space . . .

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
JAK
Level 3
Level 3
Posts: 122
Joined: Tue Feb 27, 2007 8:42 am
Location: Central Florida

TUTORIAL: All about Linux swap space . . .

Post by JAK »

Google found this extremely helpful article on swap files. It helped me solve my missing swap mount in just minutes after spending many unsuccessful hours trying to find useful answers elsewhere.
--- start ---

Linux.com

Everything Linux and Open Source
All about Linux swap space
December 03, 2007 (4:00:00 PM) - 3 months, 3 weeks ago

By: Gary Sims

When your computer needs to run programs that are bigger than your available physical memory, most modern operating systems use a technique called swapping, in which chunks of memory are temporarily stored on the hard disk while other data is moved into physical memory space. Here are some techniques that may help you better manage swapping on Linux systems and get the best performance from the Linux swapping subsystem.

Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Swapping is necessary for two important reasons. First, when the system requires more memory than is physically available, the kernel swaps out less used pages and gives memory to the current application (process) that needs the memory immediately. Second, a significant number of the pages used by an application during its startup phase may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other applications or even for the disk cache.

However, swapping does have a downside. Compared to memory, disks are very slow. Memory speeds can be measured in nanoseconds, while disks are measured in milliseconds, so accessing the disk can be tens of thousands times slower than accessing physical memory. The more swapping that occurs, the slower your system will be. Sometimes excessive swapping or thrashing occurs where a page is swapped out and then very soon swapped in and then swapped out again and so on. In such situations the system is struggling to find free memory and keep applications running at the same time. In this case only adding more RAM will help.

Linux has two forms of swap space: the swap partition and the swap file. The swap partition is an independent section of the hard disk used solely for swapping; no other files can reside there. The swap file is a special file in the filesystem that resides amongst your system and data files.

To see what swap space you have, use the command swapon -s. The output will look something like this:

Filename Type Size Used Priority
/dev/sda5 partition 859436 0 -1

Each line lists a separate swap space being used by the system. Here, the 'Type' field indicates that this swap space is a partition rather than a file, and from 'Filename' we see that it is on the disk sda5. The 'Size' is listed in kilobytes, and the 'Used' field tells us how many kilobytes of swap space has been used (in this case none). 'Priority' tells Linux which swap space to use first. One great thing about the Linux swapping subsystem is that if you mount two (or more) swap spaces (preferably on two different devices) with the same priority, Linux will interleave its swapping activity between them, which can greatly increase swapping performance.

To add an extra swap partition to your system, you first need to prepare it. Step one is to ensure that the partition is marked as a swap partition and step two is to make the swap filesystem. To check that the partition is marked for swap, run as root:

fdisk -l /dev/hdb

Replace /dev/hdb with the device of the hard disk on your system with the swap partition on it. You should see output that looks like this:

Device Boot Start End Blocks Id System
/dev/hdb1 2328 2434 859446 82 Linux swap / Solaris

If the partition isn't marked as swap you will need to alter it by running fdisk and using the 't' menu option. Be careful when working with partitions -- you don't want to delete important partitions by mistake or change the id of your system partition to swap by mistake. All data on a swap partition will be lost, so double-check every change you make. Also note that Solaris uses the same ID as Linux swap space for its partitions, so be careful not to kill your Solaris partitions by mistake.

Once a partition is marked as swap, you need to prepare it using the mkswap (make swap) command as root:

mkswap /dev/hdb1

If you see no errors, your swap space is ready to use. To activate it immediately, type:

swapon /dev/hdb1

You can verify that it is being used by running swapon -s. To mount the swap space automatically at boot time, you must add an entry to the /etc/fstab file, which contains a list of filesystems and swap spaces that need to be mounted at boot up. The format of each line is:

<file system> <mount point> <type> <options> <dump> <pass>

Since swap space is a special type of filesystem, many of these parameters aren't applicable. For swap space, add:

/dev/hdb1 none swap sw 0 0

where /dev/hdb1 is the swap partition. It doesn't have a specific mount point, hence none. It is of type swap with options of sw, and the last two parameters aren't used so they are entered as 0.

To check that your swap space is being automatically mounted without having to reboot, you can run the swapoff -a command (which turns off all swap spaces) and then swapon -a (which mounts all swap spaces listed in the /etc/fstab file) and then check it with swapon -s.
Swap file

As well as the swap partition, Linux also supports a swap file that you can create, prepare, and mount in a fashion similar to that of a swap partition. The advantage of swap files is that you don't need to find an empty partition or repartition a disk to add additional swap space.

To create a swap file, use the dd command to create an empty file. To create a 1GB file, type:

dd if=/dev/zero of=/swapfile bs=1024 count=1048576

/swapfile is the name of the swap file, and the count of 1048576 is the size in kilobytes (i.e. 1GB).

Prepare the swap file using mkswap just as you would a partition, but this time use the name of the swap file:

mkswap /swapfile

And similarly, mount it using the swapon command: swapon /swapfile.

The /etc/fstab entry for a swap file would look like this:

/swapfile none swap sw 0 0

How big should my swap space be?

It is possible to run a Linux system without a swap space, and the system will run well if you have a large amount of memory -- but if you run out of physical memory then the system will crash, as it has nothing else it can do, so it is advisable to have a swap space, especially since disk space is relatively cheap.

The key question is how much? Older versions of Unix-type operating systems (such as Sun OS and Ultrix) demanded a swap space of two to three times that of physical memory. Modern implementations (such as Linux) don't require that much, but they can use it if you configure it. A rule of thumb is as follows: 1) for a desktop system, use a swap space of double system memory, as it will allow you to run a large number of applications (many of which may will be idle and easily swapped), making more RAM available for the active applications; 2) for a server, have a smaller amount of swap available (say half of physical memory) so that you have some flexibility for swapping when needed, but monitor the amount of swap space used and upgrade your RAM if necessary; 3) for older desktop machines (with say only 128MB), use as much swap space as you can spare, even up to 1GB.

The Linux 2.6 kernel added a new kernel parameter called swappiness to let administrators tweak the way Linux swaps. It is a number from 0 to 100. In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle. Kernel maintainer Andrew Morton has said that he runs his desktop machines with a swappiness of 100, stating that "My point is that decreasing the tendency of the kernel to swap stuff out is wrong. You really don't want hundreds of megabytes of BloatyApp's untouched memory floating about in the machine. Get it out on the disk, use the memory for something useful."

One downside to Morton's idea is that if memory is swapped out too quickly then application response time drops, because when the application's window is clicked the system has to swap the application back into memory, which will make it feel slow.

The default value for swappiness is 60. You can alter it temporarily (until you next reboot) by typing as root:

echo 50 > /proc/sys/vm/swappiness

If you want to alter it permanently then you need to change the vm.swappiness parameter in the /etc/sysctl.conf file.
Conclusion

Managing swap space is an essential aspect of system administration. With good planning and proper use swapping can provide many benefits. Don't be afraid to experiment, and always monitor your system to ensure you are getting the results you need.

Read in the original layout at: http://www.linux.com/feature/121916
.
Husse

Re: TUTORIAL: All about Linux swap space . . .

Post by Husse »

Thanks - didn't think it was that hard
swap on swap off
and that's it :)
JAK
Level 3
Level 3
Posts: 122
Joined: Tue Feb 27, 2007 8:42 am
Location: Central Florida

Re: TUTORIAL: All about Linux swap space . . .

Post by JAK »

Husse wrote:Thanks - didn't think it was that hard
swap on swap off
and that's it :)
For whatever reason swapon-swapoff wasn't working for me. I should have consulted Mr. Miogi. I had to run through the entire tutorial to make it work.... besides, I'm just a noob as are many people these days not looking to support Microsoft anymore.
User avatar
linuxviolin
Level 8
Level 8
Posts: 2081
Joined: Tue Feb 27, 2007 6:55 pm
Location: France

Re: TUTORIAL: All about Linux swap space . . .

Post by linuxviolin »

JAK wrote:1) for a desktop system, use a swap space of double system memory, as it will allow you to run a large number of applications (many of which may will be idle and easily swapped), making more RAM available for the active applications
Yes but with at least 1 GB of RAM I guess 1 GB swap should be sufficient, if the PC is configured properly :roll: (but I can be wrong :lol: )

About swappiness, for me Morton is wrong. I always change for 0 or 1, 60 is crazy! It sucks! (of course it depends on your RAM but with at least 1GB of RAM - as me, 1GB - you should change it)
Also you can/should change 'vfs_cache_pressure' for e.g 50 (for this parameter you should experiment)

For some explanations on swappiness, vfs_cache_pressure to accelerate Linux you can look at http://rudd-o.com/archives/2007/10/02/t ... -fix-that/

You can also see at http://www.kernel.org/pub/linux/kernel/ ... ning.patch for some info on vfs_cache_pressure

For cache_pressure we really need to do some blind experiments. man command is our friend, this is the truth but this is one of the places where the Linux kernel sucks, because it doesn't quite follow the claimed philosophy. man sysctl and man sysctl.conf DON'T DOCUMENT ANY of the system variables! :twisted:


For a permanent change you just need to add at the end of your /etc/systcl.conf file and without '#' (example from my file):

Code: Select all

vm.swappiness=0
vm.vfs_cache_pressure=50
With the number of your choice of course..... :D

Ah and to implement these parameters immediately without reboot, type in a terminal:

Code: Select all

sudo /sbin/sysctl -p
K.I.S.S. ===> "Keep It Simple, Stupid"
"Simplicity is the ultimate sophistication." (Leonardo da Vinci)
"Everything should be made as simple as possible, but no simpler." (Albert Einstein)
Fred

Re: TUTORIAL: All about Linux swap space . . .

Post by Fred »

linuxviolin,

You are going to take on this topic? Wow... You are a brave man. :-)

When I first read this one sometime ago, I didn't even know where to begin, so I just left it.

I will follow this one with interest. :-)

Fred
Husse

Re: TUTORIAL: All about Linux swap space . . .

Post by Husse »

Currently I'm testing

Code: Select all

    vm.swappiness=0
    vm.vfs_cache_pressure=50
:lol: :lol:
Fred

Re: TUTORIAL: All about Linux swap space . . .

Post by Fred »

I would pay close attention to linuxviolin's posts on this issue. So far he is right on the money. :-)

Fred
AK Dave

Re: TUTORIAL: All about Linux swap space . . .

Post by AK Dave »

What to set swappiness to should depend on the quality/speed of the swapfile, the RAM available on the computer, and the demands on said RAM in both "typical" and "maximal" system load scenarios.

I'm familar with swappiness. I'm not familiar with vfs_cache_pressure. I need to read up on that one.

My laptop, for example, has 2gb RAM and runs pretty lean. I rarely tax RAM at all with typical usage. I have a 1.8gb swap partition on the hard disk with swappiness=0. I have a 1gb swap partition on a USB stick that I will insert when I predict the need for more swap usage OR when I observe that I'm starting to use my swap. I "swap on" that partition with a ridiculously high priority and set swappiness=100 at the same time. It is almost as good as plopping in an extra gig of RAM.

The "desktop rig" at home that my "locals users" share has 1gb of ram and two 1.5gb swapfiles set at identical priority on two seperate physical drives with swappiness=60. That is "pretty good". There is absolutely a big performance boost IF you use swap (with 1gb ram, that happens) by having the swap distributed on two drives. Swap interlave is nice, but flash ram swap is even nicers. I can get a line on some cheap DDR 1gb boards I'd love to upgrade the ram, but I'd do even better by freeing up a USB port on that computer and permenantly dedicating a 2gb stick for swapfile use.

A decent quality thumbdrive on a USB 2.0 port is the best swapfile you can have. If you have to have a swapfile.
routine

Re: TUTORIAL: All about Linux swap space . . .

Post by routine »

AK Dave wrote:A decent quality thumbdrive on a USB 2.0 port is the best swapfile you can have. If you have to have a swapfile.
Now that is a great idea!
donec
Level 4
Level 4
Posts: 333
Joined: Sun Nov 18, 2007 5:21 pm
Location: Texas
Contact:

Re: TUTORIAL: All about Linux swap space . . .

Post by donec »

AK Dave wrote:My laptop, for example, has 2gb RAM and runs pretty lean. I rarely tax RAM at all with typical usage. I have a 1.8gb swap partition on the hard disk with swappiness=0. I have a 1gb swap partition on a USB stick that I will insert when I predict the need for more swap usage OR when I observe that I'm starting to use my swap. I "swap on" that partition with a ridiculously high priority and set swappiness=100 at the same time. It is almost as good as plopping in an extra gig of RAM.

A decent quality thumbdrive on a USB 2.0 port is the best swapfile you can have. If you have to have a swapfile.
How did you set that up?
Don
http://bestwebstop.webcentr.net
Links and accurate information provide the best answer, while garbage in provides garbage out.ImageRegistered Linux user # 449322
Moved up to LM17 KDE
AK Dave

Re: TUTORIAL: All about Linux swap space . . .

Post by AK Dave »

donec wrote:
AK Dave wrote:My laptop, for example, has 2gb RAM and runs pretty lean. I rarely tax RAM at all with typical usage. I have a 1.8gb swap partition on the hard disk with swappiness=0. I have a 1gb swap partition on a USB stick that I will insert when I predict the need for more swap usage OR when I observe that I'm starting to use my swap. I "swap on" that partition with a ridiculously high priority and set swappiness=100 at the same time. It is almost as good as plopping in an extra gig of RAM.

A decent quality thumbdrive on a USB 2.0 port is the best swapfile you can have. If you have to have a swapfile.
How did you set that up?
How to set it up? I presume you understand that the default non-USB config of my laptop is 2gb RAM and a 1.8gb linux-swap set in fstab with swappiness=0 set in sysctl.conf, and you understand how I set that much up. So I'll explain only the USB portion.

I used these directions to make a USB swap stick:
http://ubuntuforums.org/showthread.php?t=395435

My selected device is a 2gb stick, on which I have two partitions of ~1gb each. The first is a liveUSB of Hardy i386, and the second is entirely linux-swap.

Here's a quick summary of the instructions:

Code: Select all

1- insert usb thumbdrive
2- sudo umount /media/thumbdrive
3- sudo mkswap /dev/thumbdrive (/dev/sda2 for me)
4- sudo swapon -p 32767 /dev/thumbdrive (/dev/sda2 for me)
5- echo 100 > /proc/sys/vm/swappiness
Now you can use swapon -s to verify that your available swap space has expanded appropriately. The use of the -p parameter above is to force the priority of the USB swap space to be ridiculously high, and thus used first. Linux will fill swap in the order of priority you assign each swap partition. Two partitions of equal priority will be used equally, or interleaved. Interleaved swap is beneficial if you have multiple physical drives and swap space on each one. Interleaved swap only adds more CPU complexity to swap management if you're caching with the random access time of flash memory (it ain't worth doing).

Code: Select all

sudo swapon -s
Once you've created a USB swap thumbdrive/partition, you don't need to repeat steps 1-3. Insert the thumbdrive and repeat steps 4-5.

When you remove the drive, don't forget:

Code: Select all

echo xx > /proc/sys/vm/swappiness
You could make a bash script to automate some of this. That would be cool. Please share if you do. I do things the "hard way" on my laptop because I only insert USB sticks when I actually need to do so (too easy to break 'em off), habitually avoid rebooting my laptop (love suspend) and store it without anything in the USB ports. So I need to be able to add/remove the USB swap when I need it. Live would be much simpler with a USB stick in the back of a desktop computer case left there as a permenant addition and the swap defined in fstab. This is a good option for my "family rig", a desktop PC with only 1gb RAM onboard.

There is another way to do roughly the same thing with a bash script called "swapboost" that you can read about here, but I don't use it, recommend it, or endorse it:
http://ubuntu-tutorials.com/2007/07/02/ ... rs-wanted/
donec
Level 4
Level 4
Posts: 333
Joined: Sun Nov 18, 2007 5:21 pm
Location: Texas
Contact:

Re: TUTORIAL: All about Linux swap space . . .

Post by donec »

Thanks AK Dave, for your reply. I found I could use GParted to format my USB flash drive as swap and then when I need to use it I just open GParted and right click on the USB drive and select swapon. When I am finished I just open Gparted right click on the USB drive and select swapoff. Works great.
Don
http://bestwebstop.webcentr.net
Links and accurate information provide the best answer, while garbage in provides garbage out.ImageRegistered Linux user # 449322
Moved up to LM17 KDE
routine

Re: TUTORIAL: All about Linux swap space . . .

Post by routine »

Ok, so I've been doing a little reading.....

The fastest USB drives I can find (Lexar Lightning: http://www.crucial.com/store/partspecs. ... JDP4GB-231) write at about 20 mb/s and read at about 30 mb/s.

If my hard drive does it twice as fast or more, what's the benefit of using the USB drive (other than more swap space)? What would the benefit be in using a USB drive as your only swap space?

Thx.
routine

Re: TUTORIAL: All about Linux swap space . . .

Post by routine »

Hi,

I found my answer here: http://blogs.msdn.com/tomarcher/archive ... 15199.aspx ....which I got to from the Ubuntu thread that AK Dave posted: http://ubuntuforums.org/showthread.php?t=395435
Q: Aren't Hard Disks faster than flash? My HDD has 80MB/sec throughput.
A: Hard drives are great for large sequential I/O. For those situations, ReadyBoost gets out of the way. We concentrate on improving the performance of small, random I/Os, like paging to and from disk.
AK Dave

Re: TUTORIAL: All about Linux swap space . . .

Post by AK Dave »

Yes, it is a matter of random-access versus sustained throughput. Flash memory is better for small random access. It is also a matter of being able to use a seperate physical drive, so that the head doing the read/write function for linux-swap is independant of the head doing read/write for the application being loaded. Or with flash, is nonexistent in the first place.

If you have many different physical drives, make use of them. Configure a little linux-swap on each drive, fstab them with identical priority, and linux will interleave them intelligently. Large swapping and sustained throughput read/write will indeed be faster. My laptop has one hard disk, IDE 5400rpm, so even if sustained throughput from the drive is faster than a flash drive it is still faster to write to flash while reading from the drive (caching from ram to flash while loading data from the drive, as happens with swapping). It is still faster to read random data from flash than have a drive head bouncing randomly around looking for data.

Caveat:
flash swap (flash cache) on a USB stick is not identical to Vista's Readyboost. Readyboost will test the USB stick, automount the cache, and mirror to the hard disk all of the data cached to USB to prevent loss of data due to user stupidity. If PEBKAC, linux flash swap to USB is not advisable. Use a good quality USB stick, certify it yourself if you're in doubt or bought it cheap from Walmart for $12.97/gb, and don't yank the stick prematurely. Always "swapoff" and "unmount" before removing the stick or you risk loss of data.

Linux allows you to take additional risks with your own data and is no guard against PEBKAC.
User avatar
linuxviolin
Level 8
Level 8
Posts: 2081
Joined: Tue Feb 27, 2007 6:55 pm
Location: France

Re: TUTORIAL: All about Linux swap space . . .

Post by linuxviolin »

AK Dave, you have a laptop with 2 GB RAM and 1,8 GB swap plus 1 GB swap on a USB stick. Total: 2,8 GB of swap.
You have also a desktop with 1GB RAM and two 1,5 swapfiles. Total: 3 GB of swap.

If I have read well it should be that.

What you do with your PCs to need as much swap? :roll:

I find this quite large as e.g with 2 GB RAM one can almost run Linux without swap...

Just curious :D
K.I.S.S. ===> "Keep It Simple, Stupid"
"Simplicity is the ultimate sophistication." (Leonardo da Vinci)
"Everything should be made as simple as possible, but no simpler." (Albert Einstein)
AK Dave

Re: TUTORIAL: All about Linux swap space . . .

Post by AK Dave »

linuxviolin wrote:I find this quite large as e.g with 2 GB RAM one can almost run Linux without swap...
I do. Recall please that the laptop with 2gb physical ram runs routinely with swappiness=0. Day in, day out, swappiness=0. The swapfile exists, but isn't used. With swappiness=0, you only swap if you're physically out of RAM. I could unmount the partition, gparted the space to something else, and have no swap at all. You're right: linux doesn't need swap if it has sufficient ram.

I keep a loose eye on system ram, especially when I'm loading it up with a lot of work. Once or twice a month I'll be doing something on the laptop that causes a 100-200k of swap to become active. The system only caches if in dire need, so there must be some need if there is any swap used at all. If I see the system is starting to load swap, either the ram is getting used up or something has obnoxiously preallocated a huge chunk of ram. Either way, if I'm going to use swap I want it to be fast swap so I don't notice the impact. Thus, flash swap. Or I'm just letting the system churn with some menial task under conditions where I don't care to interact with the computer. In which case the disc swap is more than adequate.

The desktop rig, the family rig, with dual interleaved swap partitions on seperate physical drives, swaps very little even with only 1gb of ram because the kids and my wife don't tax it to do much more than run firefox, openoffice, and/or play some games. And they're not savvy enough to know about multiple desktops and having lots of things running at once. They're used to Windows, where such behavior is asking for trouble. If that system does swap, interleaved dual partitions make for a very fast experience.
User avatar
linuxviolin
Level 8
Level 8
Posts: 2081
Joined: Tue Feb 27, 2007 6:55 pm
Location: France

Re: TUTORIAL: All about Linux swap space . . .

Post by linuxviolin »

Ok, no problem with that. :wink:

But
Once or twice a month I'll be doing something on the laptop that causes a 100-200k of swap to become active
Just 100-200k and just "once or twice a month"? But then why have built 2,8 GB of swap (or even 1,8 GB)? This is not a bit too? :D ("swappiness=0. [...] The swapfile exists, but isn't used.")
Maybe are you so anxious that this reassures you? :lol: :lol: :lol:
With swappiness=0, you only swap if you're physically out of RAM.
Yes. Of course I know that. :wink:
K.I.S.S. ===> "Keep It Simple, Stupid"
"Simplicity is the ultimate sophistication." (Leonardo da Vinci)
"Everything should be made as simple as possible, but no simpler." (Albert Einstein)
routine

Re: TUTORIAL: All about Linux swap space . . .

Post by routine »

So, what's the recommendation? 2 gigs or more, no swap file needed? 1 gig, just in case? This is going to take some experimenting, I think.
AK Dave

Re: TUTORIAL: All about Linux swap space . . .

Post by AK Dave »

linuxviolin wrote:Just 100-200k and just "once or twice a month"? But then why have built 2,8 GB of swap (or even 1,8 GB)? This is not a bit too?
Thats my threshold where I start noticing swap being used. I normally don't use, want, or need swap. The USB stick is not inserted or used at all under normal circumstances. Normally, swap could be unmounted for all it isn't used. But under ABnormal excessive loads, swap will be used even with swappiness=0, and then I have the choice: stick with hard disk swap because the performance impact is negligible for what I'm doing; or, if I've already observed a performance hit based on whatever I'm doing at the time then I'll bridge up the swapfile with the USB stick. Then remove it.

USB swap is observably much faster than hard disk swap, especially on a single disk laptop system where there are other hardware performance bottlenecks.

Bottleneck example: I get roughly 7mb/sec copying a file from one partition to another as reported by Nautilus during large file copy procedures.

Its a laptop. Hard disk access sucks. Offload the random read/write to a USB stick and everything is happier. I don't use swap unless I absolutely have to, and then I offload the swap to USB to avoid the hard drive. The only reason there is a 1.8gb swap partition on the hard disk is as a safety net.
Post Reply

Return to “Tutorials”