Linux Swap filesystem
Generally with huge memory space on modern computers role of swap is
diminished. In most cases on servers with memory above 8G you can create
swap partition equal to the memory. There are, of course, exceptions.
Oracle is one such exception and Oracle installer generally requires 2x
physical RAM amount of swap. You can provide this amount of space in
several swap partitions (one of each mirrored pair of disks) as
allocating large swap partition of a single disk is pretty taxing
(database servers usually have at least two or three pair of mirrored
disks).
General rule from Red Hat states that
Swap should equal 2x physical RAM for up to 2 GB of physical RAM, and then
an additional 1x physical RAM for any amount above 2 GB
For systems with really large amounts of RAM (more than 32 GB) you can likely
get away with a smaller swap partition (around 1x, or less, of physical RAM).
As for whether to use swapfile or swap partition on desktops with
low loads it does not matter and swap file in more convenient (typically
Linux desktops has one large or two large partition which increase
efficiency of space utilization and simplify backup -- tradition that
came from Windows) . See
discussion
file vs swap partition for some useful additional considerations.
Unless you have a reason for doing otherwise, we recommend that you create
the following partitions for x86, AMD64, and Intel® 64 systems:
- A swap partition (at least 256 MB) — swap partitions are used to support
virtual memory. In other words, data is written to a swap partition when there
is not enough RAM to store the data your system is processing.
If you are unsure about what size swap partition to create, make it twice
the amount of RAM on your machine. It must be of type swap.
Creation of the proper amount of swap space varies depending on a number
of factors including the following (in descending order of importance):
- The applications running on the machine.
- The amount of physical RAM installed on the machine.
- The version of the OS.
Swap should equal 2x physical RAM for up to 2 GB of physical RAM, and then
an additional 1x physical RAM for any amount above 2 GB, but never less than
32 MB.
So, if:
M = Amount of RAM in GB, and S = Amount of swap in GB, then
If M < 2
S = M *2
Else
S = M + 2
Using this formula, a system with 2 GB of physical RAM would have 4 GB of
swap, while one with 3 GB of physical RAM would have 5 GB of swap. Creating
a large swap space partition can be especially helpful if you plan to upgrade
your RAM at a later time.
For systems with really large amounts of RAM (more than 32 GB) you can likely
get away with a smaller swap partition (around 1x, or less, of physical RAM).
Another useful discussion can be found at
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 -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
My 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 32773
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.
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/swappiness
If you want to alter it permanently then you need to change the
vm.swappiness
parameter in the /etc/sysctl.conf file.
- 20110126 : file vs swap partition ( September 17, 2009 , Answer swap )
- 20100812 : Learn Linux, 101 Create partitions and filesystems ( Learn Linux, 101 Create partitions and filesystems, Aug 12, 2010 )
- 20100812 : All About Linux Swap Part 2 Management Idea Excursion ( All About Linux Swap Part 2 Management Idea Excursion, )
- 20100812 : swapon/swapoff ( )
- 20100812 : Turn-offs and Turn-ons ( )
- 20100812 : Last Resort ( )
- 20100812 : Should You Use Twice the Amount of Ram as Swap Space ( Should You Use Twice the Amount of Ram as Swap Space, )
- 20100812 : Optimizing swap file partition [Archive] - Ubuntu Forums ( Optimizing swap file partition [Archive] - Ubuntu Forums, )
- 20100812 : Chriss Wiki blog-linux-MemoryRlimits ( Chris's Wiki blog-linux-MemoryRlimits, )
- 20100812 : Ask SlashdotAsk Slashdot Linux and Swap Optimization ( Ask SlashdotAsk Slashdot Linux and Swap Optimization, )
- 20100812 : Jim Frost ( Jim Frost, )
- 20100812 : Paging - Wikipedia, the free encyclopedia ( Paging - Wikipedia, the free encyclopedia, )
- 20100812 : Confusion over size of swap partition - MEPISlovers Forums ( Confusion over size of swap partition - MEPISlovers Forums, )
- 20100812 : Linux Partition HOWTO Partitioning requirements ( Linux Partition HOWTO Partitioning requirements, )
- 20071203 : All about Linux swap space by Gary Sims ( All about Linux swap space, Dec 03, 2007 )
- 20071203 : How big should my swap space be? ( )
- 20071203 : How big should my swap space be? ( )
- 200102 : CentOS LVM Resize • [ How2CentOS ] ( CentOS LVM Resize • [ How2CentOS ], )
celsius thomas
I have read here (Linux
Swap filesystem)
that swapping to a file is slower than swapping to a
partition , based on the following reasons
1) large files(swap file) will be somewhat fragmented forcing
additional disk/head movement in some cases and that you will
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 is
not in cache
is there any other reason that justifies this claim OR what
exactly is the tradeoff between a swap partition and a swap file
??
Sidney Lambe:
Speed is the only issue I've read about in this area. Swap
files are often said to be slower than swap partitions.
(Always assumed it had something to do with the difference
between a file and a device.)
However, I just reviewed the swapon man page and don't see
any mention of this issue.
I'd go with a partition if you have the option.
Keith Keller:
Well, in general (which the above touches on), with a swap
file the kernel has to deal with the overhead of the filesystem,
whereas with a partition there's no intervening filesystem to
get in the way.
But either way, if you're doing a lot of swapping, to the
point of thrashing, the efficiency of your swap space isn't
going to be a primary concern. :) I think you should prefer a
swap partition for organizational reasons, but if you simply
can't for some reason a swap file is fine.
September Storm:
My preference is based on error modes not on performance
issues -
Using a swap file if it is not available until the file
system is mounted. If the file system is corrupted or lost the
swap file is not available. A swap file can get included in
backups if you are not careful about exclude lists. Also a swap
file is unavailable if the drive has a hardware problem or
corrupt label. If the file is deleted the inode stays active so
the space stays used but it goes away at the next reboot by
which time you may have forgotten about an accidental deletion.
The swap file can also be renamed making it unavailable at the
next reboot.
Using a swap partition none of those issues apply. It's only
unavailable if the drive has hardware problems or if the label
is corrupted. The list of problems that make it unavailable is
much shorter.
MELBIN MATHEW:
I would like to share some difference between swap file and
swap partition.
Swap File, 1. If the hard disk is full or
corrupted,swap file will get damaged. 2. System speed get
decrease as soon the hard disk space is full. 3. It should be on
a particular location where it should not get damaged or
accidentally copied with other files. 4. It have advantage to
increase the swap space on a system that have already installed
with linux. 5. In any case if we need to increase the swap space
of the system immediately we can do it. 6. We can able to create
and keep the swap file on external device eg: Express Card SSD
7. New kernel have nearly achieved to make both swap partitions
and swap file speed closer. 8. Swap file will get fragmented.
Swap Partition 1. Reside on a separate hard disk
space. 2. Multiple Os on a single machine can share the same
partition. 3. Fragmentation is less compare to swap file. 4. If
hard disk is corrupted the swap partition would not be
functioning. 5. Reduce accident loss or corruption.
*Choice depend on the type of activity we are doing on the
system. Increasing Ram will increase the speed of the machine
than the swap system. But product price will be high.
Cheers! Melbin Mathew www.talk2melbin.com
Creating swap space
Now let's create some swap space on the /dev/sda4 partition using the
mkswap
command as shown in Listing 16.
Listing 16. Creating swap space[root@echidna ~]# mkswap /dev/sda4
Setting up swapspace version 1, size = 4192960 KiB
no label, UUID=8f5a3a05-73ef-4c78-bc56-0e9b1bcc7fdb
Note that recent versions of mkswap
show you the generated
UUID.
Unlike regular filesystems, swap partitions aren't mounted. Instead, they
are enabled using the swapon
command. Your Linux system's startup
scripts will take care of automatically enabling your swap partitions.
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 -s
swapon 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 -1
This 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 -mot
The -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 1972
There 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 1947
Now, 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.swappiness
Temporary
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=40
The 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.conf
Then 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=40
On succeeding boots, this value will be the default. We can re-read the config
file, verifying any changes:
sudo sysctl -p
Turn-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 -a
The -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 1953
Notice 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 1
Again, -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.
No, on servers you should not :-)
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, 2008
Hi 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
April 22nd, 2008, 05:47 AM [This part is optional...]
5. For your information, UUID is a new way to represent partitions. Yet, since it
has created problems in my previous system restore attempts, I have decided to revert
to the old way of mounting partitions, which in your case, would be:
# /etc/fstab: static file system information.
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
/dev/sdb2 / ext3 defaults,errors=remount-ro 0 1
/dev/sda6 none swap sw 0 0
/dev/hda /media/cdrom0 udf,iso9660 user,noauto,exec 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec 0 0
For more information on UUID's:
http://wolfger.wordpress.com/2007/08/31/ubuntu-and-uuid/
http://ubuntu-utah.ubuntuforums.org/showthread.php?t=512623
6. Regarding what swap is and the optimum swap size, it is a harddrive area used
by the system to dump part of the memory temporarily, when your physical RAM space
is filled by running programs and background services. Having to use the swap area
is not something desirable, as the time to access the hard drive is considerably
longer than the time to access RAM.
So, Ubunteros with more than enough RAM are always on the look out to minimize swap.
If you have equal to or more than 1 GB RAM, unless you are running a server or many
simultaneous memory intensive applications, then the system hardly ever uses your
swap. If you have 512 MB RAM, swap is rarely used. On the other hand, if you have
256 MB or less RAM, you will begin to notice some hard drive activity. In this context,
depending on your RAM size you may have little to no gain by transferring the swap
partition of another of your hard drives this way.
There is a system setting called swappiness that regulates how much the system should
depend on the swap. If you have more than enough physical RAM (say 1 GB), you may
consider decreasing this value to limit the redundant use of swap. To change this
value (whose default is 60 on a scale of 100) on the fly:
sysctl vm.swappiness # to view the current percentage
sudo sysctl vm.swappiness=10
and to make it permanent, edit /etc/sysctl.conf with:
gksudo gedit /etc/sysctl.conf
and add the line to the end of the file to set it automatically at boot time:
vm.swappiness=10
For more information on swap and swappiness:
http://ubuntuforums.org/showthread.php?t=727667
http://ubuntuforums.org/showpost.php?p=4537237&postcount=8
http://ubuntuforums.org/showpost.php?p=4537260&postcount=9
7. Finally, there is a terminal application "top" that lets you view the system
resources (cpu usage, memory consumption per application, free and used memory,
swap usage, etc.), which is the console counterpart of Gnome System Monitor. I suggest
you to install a similar console process viewer "htop" instead, which has more features
and exhibits more compatible output with gnome-system-monitor:
http://ubuntuforums.org/showpost.php?p=4539416&postcount=13
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 through
brk(2)/sbrk(2)
. These days, these aren't used very much; GNU
libc does most of its memory allocation using mmap()
, especially
for big blocks of memory.
ulimit -v
, the maximum size of the address space, works
but affects all mmap()
s, even of things that will
never require swap space, such as mmap()
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/meminfo
and the kernel
documentation on swap overcommit use). I don't care if a process wants to
mmap()
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....)
In some older virtual memory operating systems, space in swap backing store
is reserved when programs allocate memory for runtime data. OS vendors typically
issue guidelines about how much swap space should be allocated. 1.5 times the
installed RAM is a typical number. With a large amount of RAM, the disk space
needed for the backing store can be very large. Newer versions of these operating
systems attempt to solve this problem: for example, some
HP-UX kernels
offer a tunable swapmem_on that controls whether RAM can be used for memory
reservations. In systems with sufficient RAM, this significantly reduces the
needed space allocation for the backing store.
G'day all.
Will repartition HDD when I replace 6.5 with 7.0 to create a separate home partition.
The handbook for 7.0 recommends the size of the swap partition to be double
that of your RAM, but no larger than 1Gb.
I have a recent laptop with 2Gb of RAM. My swap partition is currently set to
4Gb which was based on my reading at that time. Subsequently found the most
common recommendations of swap size to be half of RAM, equal to RAM and twice
size of RAM. One recommendation even stated that the swap partition is probably
not necessary in these days of large RAM.
Would someone be kind enough to point me in the direction of an appropriate
size for a swap partition? Many thanks in advance.
Cheers,
Miles
Conventional wisdom creates swap space equal to the amount of
RAM.
But keep in mind that this is just a rule of thumb. It is
easily possible to create scenarios where programs have extremely large or extremely
small
working sets. For example, a simulation program with a large data set that
is accessed in a very random fashion would have almost no noticeable locality
of reference in its data segment, so its working set would be quite large.
On the other hand, a graphics program with many simultaneously opened JPEGs,
all but one iconified, would have a very large data segment. But image transformations
are all done on one single image, most of the memory occupied by the program
is not accessed. The same is true for an editor with many editor windows where
only one window is being modified at a time. These programs have - if they are
designed properly - a very high locality of reference and large parts of them
can be kept swapped out without too severe performance impact. A user who never
never quits programs once launched would want a lot of swap space for the same
reason.
Servers typically are configured with more swap space
than their desktop counterparts. Even though a given amount of swap is sufficient
for its operations, the server might come under transient heavy loads which
cause it to page out at a high rate. Some administrators prefer this to the
server crashing altogether. In these cases, swap might be several times the
size of ram.
How large can my swap space be?
Currently, the maximum size of a swap partition is architecture-dependent. For
i386, m68k, ARM and PowerPC, it is
"officially" 2Gb. It is 128Gb on alpha, 1Gb on sparc, and 3Tb on sparc64.
An opteron on the 2.6 kernel can write to a 16 Tb swap partition. For linux
kernels 2.1 and earlier, the limit is 128Mb. The partition may be larger than
128 MB, but excess space is never used. If you want more
than 128 MB of swap for a 2.1 and earlier kernel, you
have to create multiple swap partitions (8 max). After 2.4, 32 swap areas are
"officially" possible. See
setting up swap for details.
Where should I put my swap space?
The short answer is anywhere is fine. However, if you are interested in extracting
as much speed as possible, there are two basic strategies (other than buying
more RAM):
- Split the swap space across multiple drives,
or at least on the drive you write to least.
- Put each swap partition on the outer tracks.
Here are the considerations:
- Choice of drive
If you have a disk with many heads and one with less heads and both are
identical in other parameters, the disk with many heads will be faster.
Reading data from different heads is fast, since it is purely electronic.
Reading data from different tracks is slow, since it involves physically
moving the head.
It follows then that writing swap on a separate drive will be faster than
moving the head back and forth on a single drive.
- Placement:
Older disks have the same number of sectors on all tracks. With these disks
it will be fastest to put your swap in the middle of the disks, assuming
that your disk head will move from a random track towards the swap area.
Newer disks use ZBR (zone bit recording). They have more sectors on the
outer tracks. With a constant number of rpms, this yields a far greater
performance on the outer tracks than on the inner ones. Put your swap on
the fast tracks. (In general, low-numbered cylinders are associated low
partition numbers. However, see Kristian's more recent
comments on this issue. -Tony)
- Usage:
Of course your disk head will not move randomly. If you have swap space
in the middle of a disk between a constantly busy home partition and an
almost unused archive partition, you would be better of if your swap were
near the home partition for even shorter head movements. You would be even
better off, if you had your swap on another otherwise unused disk, though.
- Striping:
Speed can be increased by writing to multiple swap areas simultaneously.
Swap spaces with the same priority will be written to like a RAID. See
setting up swap for details.
Summary: Put your swap on a fast disk with many heads that is not
busy doing other things. If you have multiple disks: Split swap and scatter
it over all your disks or even different controllers.
max swap size: With kernel 2.4, the limit is 64
swap spaces at a maximum of 64Gb each, although this is not reflected in the
man page for mkswap
. With the 64 bit opteron on the 2.6 kernel,
128 swap areas are permitted, each a whopping 16 Tb! (thanks to Peter Chubb
for the calculation)
December 03, 2007 |
Linux.comWhen 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:
- 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/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.
Gary Sims has a degree in Business Information Systems from a British university.
He worked for 10 years as a software engineer and is now a freelance Linux consultant
and writer.(4:00:00 PM)
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:
- 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/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.
Gary Sims has a degree in Business Information Systems from a British university.
He worked for 10 years as a software engineer and is now a freelance Linux consultant
and writer.
CentOS LVM Resize
November 3, 2010
17 Comments
Since the release of CentOS 5.5 we noticed that the
default CentOS install assigns a considerable amount of
its available storage space to Swap. If you don't catch
this during the installation don't worry, Logical Volume
Manager or LVM will rectify this post install.
LVM is a logical volume manager for the Linux kernel;
it manages disk drives and similar mass-storage devices,
in particular large ones. The term "volume" refers to a
disk drive or partition thereof.
Before we start just some general housekeeping. The
XEN virtual CentOS 5.5 server (base install) in this
tutorial was assigned 10GB of storage with the default
partitioning layout.
Let check the amount of disk space available on the
file system.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
4.9G 2.4G 2.3G 51% /
/dev/xvda1 99M 23M 72M 25% /boot
tmpfs 4.0G 0 4.0G 0% /dev/shm
Let see the attributes of the logical volumes like
size, read/write status, snapshot information
# lvdisplay
- Logical volume -
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID oFXKGg-nBPo-Fk27-3ino-zaHZ-cEcv-fk0dS0
LV Write Access read/write
LV Status available
# open 1
LV Size 5.00 GB
Current LE 160
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:0
- Logical volume -
LV Name /dev/VolGroup00/LogVol01
VG Name VolGroup00
LV UUID Ntffw7-dqPh-Rjhy-rWLv-BGA0-iGik-LoyHET
LV Write Access read/write
LV Status available
# open 1
LV Size 4.88 GB
Current LE 156
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:1
Let's begin but first turning off swap on the Swap
Logical Volume
# swapoff /dev/VolGroup00/LogVol01
Once swap is off let's take the disk space we
require.
# lvresize -L -4GB /dev/VolGroup00/LogVol01
WARNING: Reducing active logical volume to 896.00 MB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LogVol01? [y/n]: y
Reducing logical volume LogVol01 to 896.00 MB
Logical volume LogVol01 successfully resized
Now let's add what we removed to the main Logical
Volume.
# lvresize -L +4GB /dev/VolGroup00/LogVol00
Extending logical volume LogVol00 to 9.00 GB
Logical volume LogVol00 successfully resized
Resize the File System
# resize2fs -p /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on
/; on-line resizing required
Performing an on-line resize of
/dev/VolGroup00/LogVol00 to 2359296 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now
2359296 blocks long.
Rebuild the swap partition.
# mkswap /dev/VolGroup00/LogVol01
Setting up swapspace version 1, size = 939520 kB
Turn swap on.
# swapon /dev/VolGroup00/LogVol01
Let's check the amount of disk space available and
LVM attributes to see if our changes took effect.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
8.8G 2.4G 6.0G 28% /
/dev/xvda1 99M 23M 72M 25% /boot
tmpfs 4.0G 0 4.0G 0% /dev/shm
# lvdisplay
- Logical volume -
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID oFXKGg-nBPo-Fk27-3ino-zaHZ-cEcv-fk0dS0
LV Write Access read/write
LV Status available
# open 1
LV Size 9.00 GB
Current LE 288
Segments 2
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:0
- Logical volume -
LV Name /dev/VolGroup00/LogVol01
VG Name VolGroup00
LV UUID Ntffw7-dqPh-Rjhy-rWLv-BGA0-iGik-LoyHET
LV Write Access read/write
LV Status available
# open 1
LV Size 896.00 MB
Current LE 28
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:1
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.
Society
Groupthink :
Two Party System
as Polyarchy :
Corruption of Regulators :
Bureaucracies :
Understanding Micromanagers
and Control Freaks : Toxic Managers :
Harvard Mafia :
Diplomatic Communication
: Surviving a Bad Performance
Review : Insufficient Retirement Funds as
Immanent Problem of Neoliberal Regime : PseudoScience :
Who Rules America :
Neoliberalism
: The Iron
Law of Oligarchy :
Libertarian Philosophy
Quotes
War and Peace
: Skeptical
Finance : John
Kenneth Galbraith :Talleyrand :
Oscar Wilde :
Otto Von Bismarck :
Keynes :
George Carlin :
Skeptics :
Propaganda : SE
quotes : Language Design and Programming Quotes :
Random IT-related quotes :
Somerset Maugham :
Marcus Aurelius :
Kurt Vonnegut :
Eric Hoffer :
Winston Churchill :
Napoleon Bonaparte :
Ambrose Bierce :
Bernard Shaw :
Mark Twain Quotes
Bulletin:
Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient
markets hypothesis :
Political Skeptic Bulletin, 2013 :
Unemployment Bulletin, 2010 :
Vol 23, No.10
(October, 2011) An observation about corporate security departments :
Slightly Skeptical Euromaydan Chronicles, June 2014 :
Greenspan legacy bulletin, 2008 :
Vol 25, No.10 (October, 2013) Cryptolocker Trojan
(Win32/Crilock.A) :
Vol 25, No.08 (August, 2013) Cloud providers
as intelligence collection hubs :
Financial Humor Bulletin, 2010 :
Inequality Bulletin, 2009 :
Financial Humor Bulletin, 2008 :
Copyleft Problems
Bulletin, 2004 :
Financial Humor Bulletin, 2011 :
Energy Bulletin, 2010 :
Malware Protection Bulletin, 2010 : Vol 26,
No.1 (January, 2013) Object-Oriented Cult :
Political Skeptic Bulletin, 2011 :
Vol 23, No.11 (November, 2011) Softpanorama classification
of sysadmin horror stories : Vol 25, No.05
(May, 2013) Corporate bullshit as a communication method :
Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law
History:
Fifty glorious years (1950-2000):
the triumph of the US computer engineering :
Donald Knuth : TAoCP
and its Influence of Computer Science : Richard Stallman
: Linus Torvalds :
Larry Wall :
John K. Ousterhout :
CTSS : Multix OS Unix
History : Unix shell history :
VI editor :
History of pipes concept :
Solaris : MS DOS
: Programming Languages History :
PL/1 : Simula 67 :
C :
History of GCC development :
Scripting Languages :
Perl history :
OS History : Mail :
DNS : SSH
: CPU Instruction Sets :
SPARC systems 1987-2006 :
Norton Commander :
Norton Utilities :
Norton Ghost :
Frontpage history :
Malware Defense History :
GNU Screen :
OSS early history
Classic books:
The Peter
Principle : Parkinson
Law : 1984 :
The Mythical Man-Month :
How to Solve It by George Polya :
The Art of Computer Programming :
The Elements of Programming Style :
The Unix Hater’s Handbook :
The Jargon file :
The True Believer :
Programming Pearls :
The Good Soldier Svejk :
The Power Elite
Most popular humor pages:
Manifest of the Softpanorama IT Slacker Society :
Ten Commandments
of the IT Slackers Society : Computer Humor Collection
: BSD Logo Story :
The Cuckoo's Egg :
IT Slang : C++ Humor
: ARE YOU A BBS ADDICT? :
The Perl Purity Test :
Object oriented programmers of all nations
: Financial Humor :
Financial Humor Bulletin,
2008 : Financial
Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related
Humor : Programming Language Humor :
Goldman Sachs related humor :
Greenspan humor : C Humor :
Scripting Humor :
Real Programmers Humor :
Web Humor : GPL-related Humor
: OFM Humor :
Politically Incorrect Humor :
IDS Humor :
"Linux Sucks" Humor : Russian
Musical Humor : Best Russian Programmer
Humor : Microsoft plans to buy Catholic Church
: Richard Stallman Related Humor :
Admin Humor : Perl-related
Humor : Linus Torvalds Related
humor : PseudoScience Related Humor :
Networking Humor :
Shell Humor :
Financial Humor Bulletin,
2011 : Financial
Humor Bulletin, 2012 :
Financial Humor Bulletin,
2013 : Java Humor : Software
Engineering Humor : Sun Solaris Related Humor :
Education Humor : IBM
Humor : Assembler-related Humor :
VIM Humor : Computer
Viruses Humor : Bright tomorrow is rescheduled
to a day after tomorrow : Classic Computer
Humor
The Last but not Least Technology is dominated by
two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt.
Ph.D
Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org
was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP)
without any remuneration. This document is an industrial compilation designed and created exclusively
for educational use and is distributed under the Softpanorama Content License.
Original materials copyright belong
to respective owners. Quotes are made for educational purposes only
in compliance with the fair use doctrine.
FAIR USE NOTICE This site contains
copyrighted material the use of which has not always been specifically
authorized by the copyright owner. We are making such material available
to advance understanding of computer science, IT technology, economic, scientific, and social
issues. We believe this constitutes a 'fair use' of any such
copyrighted material as provided by section 107 of the US Copyright Law according to which
such material can be distributed without profit exclusively for research and educational purposes.
This is a Spartan WHYFF (We Help You For Free)
site written by people for whom English is not a native language. Grammar and spelling errors should
be expected. The site contain some broken links as it develops like a living tree...
Disclaimer:
The statements, views and opinions presented on this web page are those of the author (or
referenced source) and are
not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness
of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be
tracked by Google please disable Javascript for this site. This site is perfectly usable without
Javascript.
Last modified:
March 12, 2019-