How do I add more swap?
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. Swap can be added by increasing the size of the swap partition or by adding a swap file. Keep in mind that when creating a swap file that it may not necessarily be using contiguous disk blocks (as a swap partition will), and this could have a negative impact on performance as disk access times may be longer, and the more your system uses swap, the worse it will be. The Linux kernel also accesses swap disk block IO directly bypassing all caching, metadata and filesystem code, so a swap file should have no ill effect on the stability of your base filesystem. Since kernel 2.6.29 the swap system has automatically supported TRIM capable devices like SSDs.
The advantages of a swap file are many, but it is problematic for using the default "swsusp" hibernation method for powerless sleep. The best solution for maintaining hibernate capability after adding RAM is to increase the size of the swap partition.
Process to Increase Size of Swap Partition and use it for Hibernation
- Creating the swap partition
- Activating the swap partition
- Making the new swap partition work for hibernate (optional)
Creating the swap partition
- Boot to Ubuntu install CD (I'm on Natty) and choose the option to run Ubuntu now
- Go to system -> GParted Partition Editor
- Delete the swap partition and, if there is nothing else in it, the extended partition that holds it. (If by some miracle you're able to resize your swap partition from here, I imagine your life will be a lot easier than mine.)
- Decrease the size of your primary partition by the amount you want your new swap to be (I made mine 2x RAM + 500MB just to be safe). The easiest way to do this is to fill in the amount of space you want swap to be in the "free space following" field
- In the free space that has now been created, choose new, type linux-swap and you can name the partition "swap" if you like
- Hit the *Apply* button (should be a check mark) to write the changes to disk
- When done, reboot back into Ubuntu
Activating the swap partition
(If your swap is on your primary hard drive, you don't need to do anything here.) Now you need to find what partition your swap is on and what its UUID is. UUID?! you say? Well that's the Universally Unique IDentifier for the partition so you can reference it even if it's on a different mount point from boot-to-boot due to adding disks, etc.
- Pull up a terminal and run gksu gparted & and enter your root password. The & lets this process run while still giving you access to the command line.
- Right-click on your swap partition and choose *Information*. You should see the **Path** and **UUID** listed there. Keep this open for further reference.
- Run gksu gedit /etc/fstab & and look for the line that has *swap* in it. It should be the third column, separated by spaces or tabs. You can either use the path or the UUID to tell Linux where to find your swap partition. I recommend UUID because it'll stay constant even if you move the partition around or the disk somehow becomes sdb instead of sda or something like that. Make the appropriate edits and save the file. Your line should look something like this if you used UUID (with your UUID instead, of course):
- UUID=41e86209-3802-424b-9a9d-d7683142dab7 none swap sw 0 0
- or this if you used path: /dev/sda2 none swap sw 0 0
- Save the file.
- Enable the new swap partition with this command.
- sudo swapon --all OR
$ sudo swapon --all --verbose swapon on /dev/sda2 swapon: /dev/sda2: found swap signature: version 1, page-size 4, same byte order swapon: /dev/sda2: pagesize=4096, swapsize=2147483648, devsize=2147483648
- Confirm that the swap partition exists.
$ cat /proc/swaps Filename Type Size Used Priority /dev/sda2 partition 2097148 0 -1
- Reboot to make sure the new swap gets activated properly at startup
Making the swap partition work for hibernate (optional)
'INFO: This will not work for 12.04, resume from hibernate work differently in 12.04.'
- Pull up a Terminal again and run cat /proc/swaps and hopefully you see the path to your swap partition listed there. If not chances are something went wrong in the steps above. Here's my output:
Filename Type Size Used Priority
/dev/sda2 partition 2676732 73380 -1
- gksu gedit /etc/default/grub & to pull up the boot loader configuration
- Look for the line GRUB_CMDLINE_LINUX="" and make sure it looks like this (using your UUID of course)GRUB_CMDLINE_LINUX="resume=UUID=41e86209-3802-424b-9a9d-d7683142dab7" and save the file
- sudo update-grub and wait for it to finish
- gksu gedit /etc/initramfs-tools/conf.d/resume & and make sure its contents are resume=UUID=41e86209-3802-424b-9a9d-d7683142dab7 (with your UUID of course in place of mine). Save the file!
- sudo update-initramfs -u
- Reboot!
Now you should be able to hibernate and resume!
Four-step Process to Add Swap File
- Creating a file the size you want.
- Formatting that file to create a swapping device.
- Adding the swap to the running system.
- Making the change permanent.
'INFO: This will not work on btrfs-filesystems at the moment. See man swapon.'
For Adding a 512 MiB swap
- Creating a file for 512 MiB size you want:
We will create a /mnt/512MiB.swap swap file and set the permissions so that users cannot read it directly.
sudo fallocate -l 512m /mnt/512MiB.swap
sudo chmod 600 /mnt/512MiB.swap
fallocate length suffixes are: k, m, g, t, p, e (See man fallocate).
By default your swap file may be created world readable. We set the 600 mode permissions in order to prevent users from being able to read potentially sensitive information from the swap file.
If fallocate fails with "fallocate failed: Operation not supported" as it currently does on my Maverick machine, you can do this the old way, again 512 mebibytes:
sudo dd if=/dev/zero of=/mnt/512MiB.swap bs=1024 count=524288
sudo chmod 600 /mnt/512MiB.swap
- Formatting that file to create a swapping device:
sudo mkswap /mnt/512MiB.swap
- Adding the swap to the running system:
sudo swapon /mnt/512MiB.swap
The additional swap is now available and can be seen by "cat /proc/meminfo"
- Making the change permanent:
Edit the /etc/fstab:
gksudo gedit /etc/fstab
Add this line at the end of the file:
/mnt/512MiB.swap none swap sw 0 0
Save. After the next reboot the swap will be used automatically.
Example of making a swap file
This is an example of making and using a swap file on a computer with no swap partition.
user@computer:~$ sudo fallocate -l 512m /mnt/512MiB.swap
Password:
user@computer:~$ sudo mkswap /mnt/512MiB.swap
Setting up swapspace version 1, size = 536866 kB
no label, UUID=dd6a01c8-93f0-41e0-9b7a-306956d8821b
user@computer:~$ sudo swapon /mnt/512MiB.swap
user@computer:~$ cat /proc/meminfo
MemTotal: 499496 kB
MemFree: 9156 kB
Buffers: 4748 kB
Cached: 233140 kB
SwapCached: 724 kB
Active: 254432 kB
Inactive: 157920 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 499496 kB
LowFree: 9156 kB
SwapTotal: 524280 kB
SwapFree: 523556 kB
Dirty: 128 kB
Writeback: 0 kB
Mapped: 243420 kB
Slab: 20672 kB
CommitLimit: 774028 kB
Committed_AS: 648680 kB
PageTables: 2224 kB
VmallocTotal: 524280 kB
VmallocUsed: 5708 kB
VmallocChunk: 518176 kB
user@computer:~$ gksudo gedit /etc/fstab
user@computer:~$ free
total used free shared buffers cached
Mem: 499496 479488 20008 0 8256 215892
-/+ buffers/cache: 255340 244156
Swap: 524280 3856 520424
#####Then, after running a few more programs...
user@computer:~$ free
total used free shared buffers cached
Mem: 499496 492768 6728 0 1240 142336
-/+ buffers/cache: 349192 150304
Swap: 524280 53384 470896
#####Next, reboot to make sure it will work consistently.
user@computer:~$ free
total used free shared buffers cached
Mem: 499496 493136 6360 0 7528 174700
-/+ buffers/cache: 310908 188588
Swap: 524280 17148 507132
Undoing your changes :
Undoing basically follows the same process in reverse.
gksudo gedit /etc/fstab
Remove the line
/mnt/512MiB.swap none swap sw 0 0
Remove the swap from the running system and remove the swap file.
sudo swapoff /mnt/512MiB.swap && sudo rm /mnt/512MiB.swap
No need to reboot.
What is swappiness and how do I change it?
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.
- swappiness can have a value of between 0 and 100
- swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible
- swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache
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.
To check the swappiness value
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
sudo sysctl vm.swappiness=10
To make a change permanent, edit the configuration file with your favorite editor:
gksudo gedit /etc/sysctl.conf
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
Save the file and reboot.
What is the priority of swap containers?
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
Should I reinstall with more swap?
Definitely not. With the 2.6 kernel, "a swap file is just as fast as a swap partition."(Wikipedia:Paging, LKML).
Why is my swap not being used?
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.
Swap may not be needed
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 than ram + swap overflow.
Is there a swap partition at all?
Use this command to see all partitions
sudo fdisk -l
You should be able to see something like this in the output
/dev/hda8 4787 4870 674698+ 82 Linux swap / Solaris
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.
- Use the following command
cat /etc/fstab
- Ensure that there is a line link below. This enables swap on boot.
/dev/hda8 none swap sw 0 0
- 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:
- place the script e.g. /usr/local/sbin:
gksudo gedit /usr/local/sbin/swap2ram.sh
- copy&paste the script into the file:
err="not enough RAM to write swap back, nothing done"
mem=`free|grep Mem:|awk '{print $4}'`
swap=`free|grep Swap:|awk '{print $3}'`
test $mem -lt $swap && echo -e $err && exit 1
swapoff -a && swapon -a &&
exit 0
- save and close gedit
- make the script executable:
sudo chmod +x /usr/local/sbin/swap2ram.sh
- execute:
sudo /usr/local/sbin/swap2ram.sh
For more information :-https://help.ubuntu.com/community/SwapFaq/#How_do_I_add_more_swap.3F
No comments:
Post a Comment