|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
Softpanorama Search
|
| News | See also | Recommended Links | NTFS | Disk Repartitioning |
| Solaris File System Structure | Filesystems Recovery | Humor | Etc |
From Linux.com -- All about Linux swap space by Gary Sims
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 -1Each 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/hdbReplace /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 / SolarisIf 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/hdb1If you see no errors, your swap space is ready to use. To activate it immediately, type:
swapon /dev/hdb1You 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 0where /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 -acommand (which turns off all swap spacesMy additional information:
<root /># free -m
total used free shared buffers cached
Mem: 16023 3128 12895 0 256 2229
-/+ buffers/cache: 641 15381
Swap: 32773 0 32773How 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.
A rule of thumb is as follows:
- 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;
- 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;
- 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/swappinessIf you want to alter it permanently then you need to change the vm.swappiness parameter in the /etc/sysctl.conf file.
Old News ;-)
Note: This article is the second in a multipart series introducing the Linux swap. Part 1 is intended to familiarize the reader with the concept of swapping, why it exists, and what it’s used for. Part 2 appears below and highlights basic analysis and management techniques for handling swap space. Part 3 will discuss the current state of swap usage and present opinions about its implementation today.
Exploring Swap
If you’re new to Linux and choose a guided installation where the partitioning was automatically configured for you, you may not even be aware of how much swap space you’ve allocated and where exactly it lives. Fortunately, there are a couple different ways that allows us to explore, analyze and manage this information.
swapon/swapoff
swapon -sswapon is part of a pair of commands (its complement being swapoff) intended to wholly toggle the state of swap. Specifically, man states, “enable/disable devices and files for paging and swapping”. The -s parameter simply displays current usage. The output should look similar to this:
Filename Type Size Used Priority /dev/sda3 partition 2000084 5452 -1This result tells us a few things:
- Filename: /dev/sda3
- This is the actual device that swap lives on
- Type: partition
- This filename is actually a partition, rather than a file. Swap can live in either type of device
- Size: 2000084
- The size in KB of the device/file. In this case, 2GB
- Used: 5452
- The amount of space currently in use in KB. Just over 5MB currently
- Priority: -1
- This is an arbitrary number (typically from 0 through 10) that allows the user to configure the order in which Linux should utilize swap space (highest to lowest). Most users will only have a single swap file, but it is certainly possible to have multiple and spread them among several different files/partitions.
More information on priority: A scenario that might exist in which you would need this, is if you decide to add another swap, but have it within a file rather than partition new space for it. You can set the partition to a priority of 5 and the new swap file to Priority of 1. Because the highest value is always preferred, this will ensure that the swap partition is used before the swap file. Another case is spreading the swap over multiple separate drives. If the priority is set the same for both swap devices, the kernel will utilize them in a manner similar to a striped array (round-robin). When it comes time to write to disk, this can help increase performance a bit.
free
Another utility, free, doesn’t give us anymore information than swapon -s, but it allows us to see swap usage in the context of overall system memory.
free -motThe -m parameter simply displays the values in MB rather than KB. -t gives us a grand total at the bottom. The -o option hides some information we’ll get to shortly. The output might look something like this:
total used free shared buffers cached Mem: 3959 3934 24 0 158 1957 Swap: 1953 5 1947 Total: 5912 3940 1972There are 3 lines in the output: Memory, Swap, and the aggregate Total. This gives a better overall picture as to what memory is allocated where. Most of the lines are self-explanatory, but it’s important to note that the very low value of free memory is somewhat misleading. In this example, there is usable free memory of 2140MB.
Without getting too far into the details, think of this as saving a seat next to you in the movie theater. Maybe a friend was joining you for a movie, but in an emergency they had to leave during the previews. Most people assume this seat is reserved, until the theater begins to reach capacity, in which case the seat might really be needed. This extra seat is analogous to cached memory. It was in use at one time, and is still being saved in case your friend comes back, but could be used by another person. Modern OS’s handle memory the same way: They mark the space as used, but available.
To get a simpler picture of how much space we have, we need to add the buffers and cached columns back into the free column, because after all, the seat is technically empty. We can accomplish this automatically by removing the -o and -t parameter from our example (free -m):
total used free shared buffers cached Mem: 3959 3934 24 0 158 1957 -/+ buffers/cache: 1818 2140 Swap: 1953 5 1947Now, compare these values to the swap space. Although there is plenty of memory free, some swap is being utilized - a piddly 5MB of 1953MB - nothing to worry about. The final line, total sums things up, indicating we have 5912MB of usable space between all memory and swap, of which 3940 is in use, leaving 1972MB.
The -/+ buffers/cache sub-item simply summarizes how much used or free memory we actually have, and it becomes clear that there is plenty of available room for new applications. So why is swap being used up at all? Without exploring the gritty details, I can offer a much simpler solution: adjust how quickly swap is utilized.
Controlling swap
Because swap is intended as a supplement to memory, the process by which items are swapped in and out is automatic, giving the user little choice what goes into it. However, since kernel 2.6 a small tweak was added to configure how quickly the system swaps items - swappiness.
Swappiness is an arbitrary number from 0 to 100 that indicates how fast you want the system to page items out to disk, freeing up precious RAM. The higher the number, the sooner pages will be written out to swap. You can alter swappiness either temporarily or permanently, as outlined below. Before you go about that, check the value currently set:
sysctl vm.swappinessTemporary
A simple terminal command is all that is required to change swappiness for the current session. If you want to keep things in memory longer than the current value provides, try lowering swappiness. For example, setting a value of 40:
sudo sysctl vm.swappiness=40The nice thing about this, is that you can change the setting without rebooting and find a nice threshold that suits your needs. After restarting, the settings will be returned to the default value, so once you find a preferred value, you may decide to change swappiness permanently.
Permanent
This is only slightly harder, but not by much. It actually involved adding or editing a parameter in a single file.
sudo nano /etc/sysctl.confThen press Ctrl+W to search for “vm.swappiness”. If nano finds it, great! Just change the value from the current to your desired. If the value is not found, scroll to the bottom of sysctl.conf and add it. In either case, it should look like this:
vm.swappiness=40On succeeding boots, this value will be the default. We can re-read the config file, verifying any changes:
sudo sysctl -pTurn-offs and Turn-ons
There’s one last thing we can actually do to control swap: completely disable and enable it. This brings us back to the first section on swapon and swapoff. There are several reasons why one might want to completely turn swap off. One of those reasons is to flush whatever is in swap back to memory. As long as you actually have enough system memory for the contents of swap, you’re not going to break anything, but it might by worthwhile to ask yourself what the system might have swapped the pages out in the first place. Maybe there’s a runaway process leaking memory. Assuming everything is acting appropriately, in the long run, it is probably better to adjust swappiness as detailed above.
If you’ve decided that you want to move pages out of swap and back into memory, all you need is a couple commands, which we can combine into a single line:
sudo swapoff -a && sudo swapon -aThe -a option on both of these command stands for “auto”. To be a more specific, it tells them to operate on all swap devices located in /etc/fstab that aren’t explicitly marked at “noauto”. Don’t worry about the details for now, just know it works.
After running this command, my memory looked like this:
total used free shared buffers cached Mem: 3959 3935 23 0 159 1956 -/+ buffers/cache: 1819 2139 Swap: 1953 0 1953Notice that Swap now reports 0MB used, with a small decrease in the amount of cached and free memory. If there’s a lot of data in swap, it may take a bit of time for this to finish. Don’t panic, but instead, open another terminal and observe your memory usage:
free -ms 1Again, -m reports usage in MB. -s 1 activates a continuous polling delay of 1 second. In other words, free -m will continue to run every 1 second until you tell it to stop with Ctrl+C. If you use this while swapoff is at work, you should see all the memory numbers adjust magically.
Last Resort
If course, if all else fails, a simple reboot will clear out not only your swap, but also anything in memory.
Wrap-up
Fortunately, Linux allows us to tweak many settings and the GNU/Linux toolset gives us a great way to handle these changes. Hopefully, this article has shed some light on how to manage that mysterious swap partition and will provide a jump point to learning more about the specific commands involved.
Part 3 will be less technical, instead focusing on a discussion about modern swap usage and how that impacts the end-user today.
Linux and other Unix-like operating systems use the term "swap" to describe both the act of moving memory pages between RAM and disk, and the region of a disk the pages are stored on. It is common to use a whole partition of a hard disk for swapping. However, with the 2.6 Linux kernel, swap files are just as fast as swap partitions. Now, many admins (both Windows and Linux/UNIX) follow an old rule of thumb that your swap partition should be twice the size of your main system RAM. Let us say I've 32GB RAM, should I set swap space to 64 GB? Is 64 GB of swap space really required? How big should your Linux / UNIX swap space be?Old dumb memory managers
I think the '2x swap space' rule came from Old Solaris and Windows admins. Also, earlier memory mangers were very badly designed. There were not very smart. Today, we have very smart and intelligent memory manager for both Linux and UNIX.
Nonsense rule: Twice the size of your main system RAM for Servers
According to OpenBSD FAQ:
Many people follow an old rule of thumb that your swap partition should be twice the size of your main system RAM. This rule is nonsense. On a modern system, that's a LOT of swap, most people prefer that their systems never swap. You don't want your system to ever run out of RAM+swap, but you usually would rather have enough RAM in the system so it doesn't need to swap.
Select right size for your setup
... ... ..
My friend who is a true Oracle GURU recommends something as follows for heavy duty Oracle server with fast storage such as RAID 10:
- Swap space == Equal RAM size (if RAM < 8GB)
- Swap space == 0.50 times the size of RAM (if RAM > 8GB)
Swap will just keep running servers...
Swap space will just keep operation running for a while on heavy duty servers by swapping process. You can always find out swap space utilization using any one of the following command:
cat /proc/swaps
swapon -s
free -m
top
See how to find out disk I/O and related information under Linux. In the end, you need to add more RAM, adjust software (like controlling Apache workers or using lighttpd web server to save RAM) or use some sort of load balancing.Also, refer Linux kernel documentation for /proc/sys/vm/swappiness. With this you can fine tune swap space.
A note about Desktop and Laptop
If you are going to suspend to disk, then you need swap space more than actual RAM. For example, my laptop has 1GB RAM and swap is setup to 2GB. This only applies to Laptop or desktop but not to servers.
Kernel hackers need more swap space
If you are a kernel hacker (debugging and fixing kernel issues) and generating core dumps, you need twice the RAM swap space.
Conclusion
If Linux kernel is going to use more than 2GiB swap space at a time, all users will feel the heat. Either, you get more RAM (recommend) and move to faster storage to improve disk I/O. There are no rules, each setup and configuration is unique. Adjust values as per your requirements. Select amount of swap that is right for you.
April 22nd, 2008Hi Nick,
As you have also guessed correctly, the line to change in /etc/fstab is:
# /dev/sdb7
UUID=6af97bc7-faa8-4619-a8bf-1057d5c16c63 none swap sw 0 0
which points to /dev/sdb7. We shall have it point to /dev/sda6, which is fairly easy.
1. You need to edit /etc/fstab with root permissions. First, let us back up the old file. From a terminal:
sudo cp /etc/fstab /etc/fstab.old
And then:
gksudo gedit /etc/fstab
2. You need to replace the above two lines with:
/dev/sda6 none swap sw 0 0
3. Alternatively, you can learn the UUID code for your swap partition with:
blkid
#or with:
sudo vol_id -u /dev/sda6
and write this code in:
# /dev/sda6
UUID=............................... none swap sw 0 0
4. Once you make sure that your new swap is ready to roll, you can erase the older one(/dev/sdb7), reformat it to NTFS and add it to (dev/sdb6) by either installing GParted under Ubuntu and using it or by downloading and burning the GParted Live CD:
http://gparted.sourceforge.net/livecd.php
Limiting a process's memory usage on Linux
Due to recent events I have become interested in this issue, so I have been poking around and doing some experiments. Unfortunately, while Linux has a bewildering variety of memory related per-process resource limits that you can set, most of them don't work or don't do you any good.
What you have, in theory and practice:
ulimit -m, the maximum RSS, doesn't do anything; the kernel maintains the number but never seems to use it for anything.
ulimit -d, the maximum data segment size, is effectively useless since it only affects memory that the program obtains throughbrk(2)/sbrk(2). These days, these aren't used very much; GNU libc does most of its memory allocation usingmmap(), especially for big blocks of memory.
ulimit -v, the maximum size of the address space, works but affects allmmap()s, even of things that will never require swap space, such asmmap()ing a big file.What I really want is something that can effectively limit a process's 'committed address space' (to use the term that
/proc/meminfoand the kernel documentation on swap overcommit use). I don't care if a process wants tommap()a 50 gigabyte file, but I care a lot if it wants 50G of anonymous, unbacked address space, because the latter is what will drive the system into out-of-memory.Unfortunately I can imagine entirely legitimate reasons to want to
mmap()huge files (especially huge sparse files) on a 64-bit machine, so any limit on the total process address space on our compute servers will have to be a soft limit.Since the Linux kernel already tracks committed address space information for the whole system, it's possible that it would not be too much work to extend it to a per-process limit. (The likely fly in the ointment is that memory regions can be shared between processes, which complicates the accounting and raises questions about what you do when a process modifies a virtual memory region in a way that is legal for it but pushes another process sharing the VMA over its limit.)
by MikeBabcock on Friday July 09, @12:52AM EDT (#79)
(User Info) http://www.linuxsupportline.com/~pgp/If you have 128M of ram, for now, create two 64M (or 128M if you can spare the space, but I wouldn't) swap partitions on two different drives. If you have only one drive, make it a 128M partition near the beginning ...
Partitions:
/boot
SWAP
... others ...
... this way your kernel files are always in the first 1024 cylinders but your swap is at the fastest part of older disks (newer disks use scattering to make all reads and writes the same speed).
If you use two disks, and assuming the load on both is about the same, set the priorities to be equal in /etc/fstab:
/dev/hda2 swap swap defaults,pri=1 0 0
/dev/hdc2 swap swap defaults,pri=1 0 0
This way the Linux kernel automatically uses both partitions simultaneously for faster reads and writes. If your hard drive isn't doing anything else, you'll get twice the speed out of this.
Again, with 128M of ram, you only need 128M of swap, even for most mid-level server apps. If you want to run Slashdot on your PC, you might need more :) ...
... leave some unpartitioned space in the middle of your drive to add swap to later if you need to ... (or to quickly partition and back up data to if another partition craps out).
- Michael
There's been a lot of talk about the old limits and why they were there and whatnot. Here's a little technical background as well as my own rules-of-thumb on size and location.
The old "2x" rule of thumb was always silly. What was important was that it was *at least* 1x. Why? Not because the kernel dumped core into the swap (which was true, but almost nobody really cared to grope through the dump so it was usually immaterial), but because every page of physical memory had a shadow page in swap. This was an important performance optimization.
First, realize that in most cases UNIX systems had only about half as much RAM as would be needed to hold everything running in RAM (even ignoring cache). 100% overcommit was *normal*; you just couldn't afford enough memory. On such systems swap performance could make or break a system.
BSD systems (and others) would start writing pages to swap long before memory space was exhausted, but keep the pages in memory as well. If the system suddenly needed more RAM to satisfy a request, any pages already written to disk could be grabbed immediately since they could be restored from swap later if necessary. If the application decided it needed the page before that happened, it was still there. Thus you could seriously overcommit memory without having much impact on performance.
Lots of people get worried about the performance impact of the (possibly) unnecessary page writes. I've never understood that; the way these systems work is you go tell the controller to do the write, then you go back to doing what you were doing and it'll let you know when it's done. Thus unless you're maxing out your controller/disk there is almost no penalty for doing it -- and it's a huge, huge win when you need RAM fast.
Some systems (notably Windows 95) don't start writing pages until they need more RAM. Doing this makes application performance synchronous with swap performance in overcommit situations -- and as we all know, that's going to suck royally. The upside to this approach is that the performance of the machine is maximized when everything *does* fit -- you're not doing any extra work -- and disk footprint of the system is minimized (a big deal when disks were a really big part of the cost of the whole system).
Assuming that what people here have been saying is true, that Linux swap space is additive, then my guess is that it uses a pool system such that it sets a low-water mark on free pages such that it starts shadowing pages when the low-water mark is exceeded. This management system goes under the assumption that there's some number of pages you might need immediately, but that's smaller than the total available page pool. That was usually true even in the old BSD days and is most definitely true today (what with everybody's desktop having 64MB or more, what luxury).
Anyway this approach provides the best of both worlds: if you have enough memory to keep the free page pool below the low-water limit then you don't do any extra work, but if you're running in a tight or overcommit mode you usually have a bunch of pages that you can steal fast if you need them. But beware the situation where total VM use exceeds real memory plus swap minus free page pool size -- you'll start swapping synchronously as the ability of the system to shadow pages disappears.
My rule of thumb, now as always, is to set swap such that the total VM space is roughly twice what I think I'll ever need in day-to-day operations, but at least as large as RAM.
Why that big? Because sometimes you blow right through your typical VM usage and it's better to have the machine respond slowly than to have things fail. Now that disk is cheap there's not much reason not to just pick a pretty big number.
As for why make it as large as RAM, well, call it superstition. Quite a number of VM systems on the market today (most particularly Windows NT) perform quite poorly if you don't have at least as much swap as RAM, and some even limit available RAM to the size of swap if you do that (as BSD used to do). Since I don't always have the time (or sometimes even the documentation) to figure out which ones use which policy I just pick a number large enough that it can't ever be a problem.
Now, about swap location. Ideally you have a whole disk dedicated to swap, but these days disks are so large that you couldn't realistically make use of a whole disk (or even a tenth of one) for swap unless you're working with some really serious datasets.
So pretty much everyone splits up their disk.
So far in replies I've heard recommendations for putting the swap at the low cylinders, middle, and high. There are two things you trade off in position -- data transfer rate and head movement.
If you put the swap at the outside or inside of the disk you're going to be moving the heads a long way any time you need to swap a page, assuming that one or another filesystems on the disk is in regular use (typically the case). On the outside, however, you may get some of that back because data transfer rates are the highest. Putting it on the inside (low cylinder numbers) is idiotic -- they have the slowest transfer rates *and* you usually have to move the heads a lot. Modern disks are so fast that this isn't really much of a problem, but it's so easy to avoid that there's no reason to do it.
I suggest putting it somewhere in the middle, preferably between your two most active partitions. Since the swap partition is usually relatively small compared to filesystems you don't make seek time that much longer between filesystems, and you ensure that you're pretty close to the swap partition all the time.
Lastly, we've heard comments that claiming both that swapping to a file is slower than swapping to a partition, and that that's not true. It *is* true. Large files (eg a swap file) will be fragmented somewhat, forcing additional disk/head movement in some cases, and you'll also have to deal with metadata describing where on the disk the file blocks are. This eats up both in-system filesystem cache and causes additional disk activity while you load metadata that's not in the cache.
Swapping to a file is something I'd do in an emergency or if you need more swap and can't justify repartitioning or adding a new disk. If you can plan ahead of time you should avoid it, it will always be somewhat slower, although realistically speaking with today's systems you might not be able to tell.
(I feel like an old fogey. You wouldn't believe how cool I think it is that I can have pretty much as much memory and disk as I want. Why, when I was a young hacker, we had to swap our programs to paper using a pencil....)
Linux.com -- All about Linux swap space by Gary Sims
Talk-FAQ Linux Memory Management - Gentoo Linux Wiki
Virtual memory - Wikipedia, the free encyclopedia
Paging - Wikipedia, the free encyclopedia
The Linux Documentation Project have some documents that give in depth description of swap space issues.The Multi Disk HOWTO which is very much about tuning in general. It is way to big to quote here so I' rather suggest the interested readers look at the following chapters listed below (straight from the Multi Disk HOWTO homepage):
Note that the swap partiotn limit in old kernels depends on the architecture of the processor, namely if it is a 32- or 64-bits processor.
Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Disclaimer:
Last modified: August 12, 2009-