What Is Swap?: Openoffice
What Is Swap?: Openoffice
What Is Swap?: Openoffice
Swap space is the area on a hard disk which is part of the Virtual Memory of your machine,
which is a combination of accessible physical memory (RAM) and the swap space. Swap
space temporarily holds memory pages that are inactive. Swap space is used when your
system decides that it needs physical memory for active processes and there is insufficient
unused physical memory available. If the system happens to need more memory resources
or space, inactive pages in physical memory are then moved to the swap space therefore
freeing up that physical memory for other uses. Note that the access time for swap is slower
therefore do not consider it to be a complete replacement for the physical memory. Swap
space can be a dedicated swap partition (recommended), a swap file, or a combination of
swap partitions and swap files.
Unforeseeable Circumstances Unforeseeable events can and will happen (a program going
crazy, some action needing much more space than you thought, or any other unpredictable
combination of events). In these cases, swap can give you an extra delay to figure out what
happened, or to finish what you are working on.
Optimizing memory usage Since mechanical hard drives are considerably slower than RAM
(SSD - Solid State Drive - storage is not as slow as physical drives, but still slower than
RAM), when you need a file (be it a data file like a video, executables like Firefox, or
libraries), the Linux kernel reads the file into RAM and keeps it there, so that the next time
you need it, it is already in RAM and data access is much faster. The portions of RAM that
accelerate disk read are called "cached memory." You will notice that they make a huge
difference in terms of responsiveness. The Linux kernel automatically moves RAM reserved
by programs--but not really used--into swap, so that it can serve the better purpose of
extending cached memory.
Optimizing Swap performance Because swap space uses a disk device, this can cause
performance issues in any system that uses swap space significantly because the system
itself may also be using the same disk device at the same time that it is required for swap
operations. One way to reduce this problem is to have swap space on a different physical
drive so that the competition for that resource is either reduced or eliminated.
How much swap do I need?
As a base minimum, it's highly recommended that the swap space should be equal to the
amount of physical memory (RAM). Also, it's recommended that the swap space is twice the
amount of physical memory (RAM) depending upon the amount of hard disk space available
for the system (although this "recommendation" dates back from a time when physical RAM
was very expensive and most Unix systems ran with many processes in swap space - a
situation that hardly applies in most situations these days, but ancient Unix/Linux myths like
this "recommendation" tend to survive well past their "use by" dates). In reality, if you use
hibernation you need what was outlined the relevant paragraph above, otherwise you need
as much swap space as your system will use - which may be actually be very little in a
modern hardware setup. The only downside to having more swap space than you will
actually use is the disk space you will be reserving for it.
Example Scenarios
Low RAM and low disk space With 512 MB RAM and 30 GB hard disk, use 512 MB for swap
since RAM is very low.
Low RAM and high disk space With 512 MB RAM and 100 GB hard disk, use 1 GB for swap
since RAM is very low and hard disk space is in plenty.
High RAM and low disk space With 2 GB RAM and 30 GB hard disk, use 1 GB for swap
since hard disk space is very low.
High RAM and high disk space With 2 GB RAM and 100 GB hard disk, use 2 GB for swap
since hard disk space is plentiful.
Swap is generally associated with a swap partition, perhaps because the user is prompted to
create a swap partition at the time of installation. In fact, any file can be used as a swapping
device, be it a partition or a conventional file. Although, to improve the responsiveness, it's
recommended to have a good sized amount of RAM available. Swap can be added by
adding a swap file.
Here count=512, means we want our file to contain 512 blocks of bs=1M, which means
block size = 1 mebibyte (1 048 576 bytes). Be careful *not* to do this dd
of=/mnt/512Mb.swap bs=1M seek=512 count=0 Though the file grows to 512Mb
immediately, it will have holes that makes it unusable.
The additional swap is now available and can be seen by "cat /proc/meminfo"
Save. After the next reboot the swap will be used automatically.
This is an example of making and using a swap file on a computer with no swap partition.
No need to reboot.
The swappiness parameter controls the tendency of the kernel to move processes out of
physical memory and onto the swap disk. Because disks are much slower than RAM, this
can lead to slower response times for system and applications if processes are too
aggressively moved out of memory.
The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness
will probably improve overall performance for a typical Ubuntu desktop installation. A value of
swappiness=10 is recommended, but feel free to experiment.Note: Ubuntu server
installations have different performance requirements to desktop systems, and the default
value of 60 is likely more suitable.
cat /proc/sys/vm/swappiness
To change the swappiness value A temporary change (lost on reboot) with a swappiness
value of 10 can be made with
To make a change permanent, edit the configuration file with your favorite editor:
Search for vm.swappiness and change its value as desired. If vm.swappiness does not exist,
add it to the end of the file like so:
vm.swappiness=10
The Linux kernel assigns priorities to all swap containers. To see the priorities that the Linux
Kernel assigns to all the swap containers use this command.
cat /proc/swaps
Priorities can be changed by using the swapon command or defined in /etc/fstab. Consult the
manual page of swapon for more info
man swapon
Definitely not. With the 2.6 kernel, "a swap file is just as fast as a swap
partition."(Wikipedia:Paging, LKML).
My swap is not being used! When I issue the free command, it shows something like this:
tom@tom:~$ free
total used free shared buffers
cached
Mem: 515980 448664 67316 0 17872
246348
-/+ buffers/cache: 184444 331536
Swap: 674688 0 674688
Note: This regards mainly swap on hard disk partitions, but it could help anyway. In these
examples /dev/hda8 is considered as swap.
Start many memory consuming applications (e.g. Gimp, web browsers, OpenOffice etc) and
then issue the free command again. Is swap being used now?
Ubuntu Desktop uses Swap to Hibernate (PC off, no power needed, program states saved).
If Hibernation is important to you, have more swap space then ram + swap overflow.
sudo fdisk -l
If not, you either need to create a swapfile or create a swap partition. To create a swap
partition you can
• boot from your Ubuntu install CD, create a swap partition out of the free space on
your hard disk and then interrupt your installation.
• use Cfdisk.
Enabling a swap partition
In case you do have a swap partition, there are several ways of enabling it.
cat /etc/fstab
• Ensure that there is a line link below. This enables swap on boot.
• Then disable all swap, recreate it, then re-enable it with the following commands.
sudo swapoff -a
sudo /sbin/mkswap /dev/hda8
sudo swapon -a
Empty Swap
Even if you have lots of RAM and even if you have a low swappiness value, it is possible that
your computer swaps. This can hurt the multitasking performance of your desktop system.
You can use the following script to get the swap manually back into RAM:
• execute:
sudo swap2ram.sh
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.