特殊:Badtitle/NS100:SwapFaq:修订间差异
小无编辑摘要 |
小无编辑摘要 |
||
(未显示同一用户的1个中间版本) | |||
第7行: | 第7行: | ||
* This FAQ will tell you how much swap you need and how to add more swap after installation. | * This FAQ will tell you how much swap you need and how to add more swap after installation. | ||
* You will be given very simple answers (to prevent losing too much time reading this FAQ) and some explanations that may help you form your own opinion. For a more detailed explanation of memory and swap, see [http://distilledb.com/blog/archives/date/2009/02/22/swap-files-in-linux.page here]. | * You will be given very simple answers (to prevent losing too much time reading this FAQ) and some explanations that may help you form your own opinion. For a more detailed explanation of memory and swap, see [http://distilledb.com/blog/archives/date/2009/02/22/swap-files-in-linux.page here]. | ||
=== What is swap? === | |||
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. | |||
=== Why do I need swap? === | |||
<u>Memory consuming programs</u> | |||
Sometimes, a large program (like OpenOffice, Neverwinter Nights, or a video editor) make the entire system need extra memory. A significant number of the pages used by these large programs during its startup may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other programs or even for the disk cache. In these cases, swap will be used to help the system handle any extra load. | |||
<u>Hibernation (suspend-to-disk)</u> | |||
The hibernation feature (suspend-to-disk) writes out the contents of RAM to the swap partition before turning off the machine. Therefore, your swap partition should be at least as big as your RAM size. The hibernation implementation currently used in Ubuntu, swsusp, needs a swap or suspend partition. It cannot use a swap file on an active file system. | |||
<u>Unforeseeable Circumstances</u> | |||
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. | |||
<u>Optimizing memory usage</u> | |||
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. | |||
<u>Optimizing Swap performance</u> | |||
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? === | === 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 ==== | |||
<u>Low RAM and low disk space</u> | |||
With 512 MB RAM and 30 GB hard disk, use 512 MB for swap since RAM is very low. | |||
<u>Low RAM and high disk space</u> | |||
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. | |||
<u>High RAM and low disk space</u> | |||
With 2 GB RAM and 30 GB hard disk, use 1 GB for swap since hard disk space is very low. | |||
<u>High RAM and high disk space</u> | |||
With 2 GB RAM and 100 GB hard disk, use 2 GB for swap since hard disk space is plentiful. | |||
=== How do I add more swap? === | === How do I add more swap? === | ||
<<Anchor(addswap)>> 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. | |||
==== Four-step Process to Add Swap ==== | |||
* 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. | ||
* | <u>For Adding a 512 MB swap</u> | ||
* <u>Creating a file for 512 MB size you want:</u> | |||
* <u> | We will create a /mnt/512Mb.swap swap file. | ||
<pre><nowiki> | |||
sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512 | sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512 | ||
</nowiki></pre> | </nowiki></pre> | ||
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. | |||
* <u>Formatting that file to create a swapping device:</u> | |||
Though the file grows to 512Mb immediately, it will have holes that makes it unusable. | <pre><nowiki> | ||
* <u> | |||
sudo mkswap /mnt/512Mb.swap | sudo mkswap /mnt/512Mb.swap | ||
</nowiki></pre> | </nowiki></pre> | ||
* <u> | * <u>Adding the swap to the running system:</u> | ||
<pre><nowiki> | |||
sudo swapon /mnt/512Mb.swap | sudo swapon /mnt/512Mb.swap | ||
</nowiki></pre> | </nowiki></pre> | ||
The additional swap is now available and can be seen by "'''cat /proc/meminfo'''" | |||
* <u> | * <u>Making the change permanent:</u> | ||
Edit the /etc/fstab: | |||
<pre><nowiki> | |||
gksudo gedit /etc/fstab | gksudo gedit /etc/fstab | ||
</nowiki></pre> | </nowiki></pre> | ||
Add this line at the end of the file: | |||
<pre><nowiki> | |||
/mnt/512Mb.swap none swap sw 0 0 | /mnt/512Mb.swap none swap sw 0 0 | ||
</nowiki></pre> | </nowiki></pre> | ||
Save and reboot. | |||
=== Example of making a swap file === | ==== Example of making a swap file ==== | ||
This is an example of making and using a swap file on a computer with no swap partition. | This is an example of making and using a swap file on a computer with no swap partition. | ||
<pre><nowiki> | <pre><nowiki> | ||
第118行: | 第118行: | ||
Swap: 524280 17148 507132 | Swap: 524280 17148 507132 | ||
</nowiki></pre> | </nowiki></pre> | ||
<u>Undoing your changes :</u> | |||
Undoing basically follows the same process in reverse. | |||
<pre><nowiki> | <pre><nowiki> | ||
gksudo gedit /etc/fstab | |||
</nowiki></pre> | |||
Remove the line | |||
<pre><nowiki> | |||
/mnt/512Mb.swap none swap sw 0 0 | |||
</nowiki></pre> | </nowiki></pre> | ||
Remove the swap from the running system and remove the swap file. | |||
<pre><nowiki> | <pre><nowiki> | ||
sudo swapoff /mnt/512Mb.swap && sudo rm /mnt/512Mb.swap | |||
</nowiki></pre> | </nowiki></pre> | ||
=== | No need to reboot. | ||
The | === 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 | |||
Reducing the default value of | 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. | ||
<u>To check the swappiness value</u> | |||
<pre><nowiki> | |||
cat /proc/sys/vm/swappiness | cat /proc/sys/vm/swappiness | ||
</nowiki></pre> | </nowiki></pre> | ||
<u>To change the swappiness value</u> | |||
A temporary change (lost on reboot) with a swappiness value of 10 can be made with | A temporary change (lost on reboot) with a swappiness value of 10 can be made with | ||
<pre><nowiki> | |||
sudo sysctl vm.swappiness=10 | sudo sysctl vm.swappiness=10 | ||
</nowiki></pre> | </nowiki></pre> | ||
To make a change permanent, edit the configuration file with your favorite editor:<pre><nowiki> | To make a change permanent, edit the configuration file with your favorite editor: | ||
<pre><nowiki> | |||
gksudo gedit /etc/sysctl.conf | gksudo gedit /etc/sysctl.conf | ||
</nowiki></pre> | </nowiki></pre> | ||
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:<pre><nowiki> | 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: | ||
<pre><nowiki> | |||
vm.swappiness=10 | vm.swappiness=10 | ||
</nowiki></pre> | </nowiki></pre> | ||
Save the file and reboot. | 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. | |||
< | <pre><nowiki> | ||
cat /proc/swaps | |||
</nowiki></pre> | |||
Priorities can be changed by using the swapon command or defined in /etc/fstab. Consult the manual page of swapon for more info | |||
<pre><nowiki> | |||
man swapon | |||
</nowiki></pre> | |||
=== Should I reinstall with more swap? === | |||
Definitely not. With the 2.6 kernel, "a swap file is just as fast as a swap partition."([http://en.wikipedia.org/wiki/Paging#Linux Wikipedia:Paging], [http://lkml.org/lkml/2006/5/29/3 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: | |||
<pre><nowiki> | <pre><nowiki> | ||
tom@tom:~$ free | tom@tom:~$ free | ||
第159行: | 第176行: | ||
Swap: 674688 0 674688 | Swap: 674688 0 674688 | ||
</nowiki></pre> | </nowiki></pre> | ||
'''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? | |||
==== Is there a swap partition at all? ==== | |||
Use this command to see all partitions | |||
<pre><nowiki> | |||
sudo fdisk -l | sudo fdisk -l | ||
</nowiki></pre> | </nowiki></pre> | ||
You should be able to see something like this in the output <pre><nowiki> | You should be able to see something like this in the output | ||
<pre><nowiki> | |||
/dev/hda8 4787 4870 674698+ 82 Linux swap / Solaris | /dev/hda8 4787 4870 674698+ 82 Linux swap / Solaris | ||
</nowiki></pre> | </nowiki></pre> | ||
If not, you either need to create a swapfile | If not, you either need to [[UbuntuHelp:[addswap|create a swapfile]]] or create a swap partition. To create a swap partition you can | ||
* boot from your Ubuntu install CD | * 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 [http://en.wikipedia.org/wiki/Cfdisk Cfdisk]. | ||
==== Enabling a swap partition ==== | |||
In case you do have a swap partition, there are several ways of enabling it. | |||
* | * Use the following command | ||
<pre><nowiki> | |||
cat /etc/fstab | |||
</nowiki></pre> | |||
* Ensure that there is a line link below. This enables swap on boot. | |||
<pre><nowiki> | |||
/dev/hda8 none swap sw 0 0 | /dev/hda8 none swap sw 0 0 | ||
</nowiki></pre> | </nowiki></pre> | ||
* Then disable all swap, recreate it, then re-enable it with the following commands. | |||
* | <pre><nowiki> | ||
swapoff -a | sudo swapoff -a | ||
/sbin/mkswap /dev/hda8 | sudo /sbin/mkswap /dev/hda8 | ||
swapon -a | sudo swapon -a | ||
</nowiki></pre> | |||
=== 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/sbin: | |||
<pre><nowiki> | |||
sudo gedit /usr/sbin/swap2ram.sh | |||
</nowiki></pre> | |||
* copy&paste the script into the file: | |||
<pre><nowiki> | |||
#!/bin/sh | |||
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 | |||
</nowiki></pre> | |||
* save and close gedit | |||
* make the script executable: | |||
<pre><nowiki> | |||
sudo chmod +x swap2ram.sh | |||
</nowiki></pre> | |||
* execute: | |||
<pre><nowiki> | |||
sudo swap2ram.sh | |||
</nowiki></pre> | </nowiki></pre> | ||
---- | ---- | ||
[[category:CategoryFaq | [[category:CategoryFaq]] | ||
[[category:UbuntuHelp]] | [[category:UbuntuHelp]] |
2010年5月20日 (四) 00:19的最新版本
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/SwapFaq }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/af | • {{#if: UbuntuHelp:SwapFaq|Afrikaans| [[::SwapFaq/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/ar | • {{#if: UbuntuHelp:SwapFaq|العربية| [[::SwapFaq/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/az | • {{#if: UbuntuHelp:SwapFaq|azərbaycanca| [[::SwapFaq/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/bcc | • {{#if: UbuntuHelp:SwapFaq|جهلسری بلوچی| [[::SwapFaq/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/bg | • {{#if: UbuntuHelp:SwapFaq|български| [[::SwapFaq/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/br | • {{#if: UbuntuHelp:SwapFaq|brezhoneg| [[::SwapFaq/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/ca | • {{#if: UbuntuHelp:SwapFaq|català| [[::SwapFaq/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/cs | • {{#if: UbuntuHelp:SwapFaq|čeština| [[::SwapFaq/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/de | • {{#if: UbuntuHelp:SwapFaq|Deutsch| [[::SwapFaq/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/el | • {{#if: UbuntuHelp:SwapFaq|Ελληνικά| [[::SwapFaq/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/es | • {{#if: UbuntuHelp:SwapFaq|español| [[::SwapFaq/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/fa | • {{#if: UbuntuHelp:SwapFaq|فارسی| [[::SwapFaq/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/fi | • {{#if: UbuntuHelp:SwapFaq|suomi| [[::SwapFaq/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/fr | • {{#if: UbuntuHelp:SwapFaq|français| [[::SwapFaq/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/gu | • {{#if: UbuntuHelp:SwapFaq|ગુજરાતી| [[::SwapFaq/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/he | • {{#if: UbuntuHelp:SwapFaq|עברית| [[::SwapFaq/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/hu | • {{#if: UbuntuHelp:SwapFaq|magyar| [[::SwapFaq/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/id | • {{#if: UbuntuHelp:SwapFaq|Bahasa Indonesia| [[::SwapFaq/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/it | • {{#if: UbuntuHelp:SwapFaq|italiano| [[::SwapFaq/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/ja | • {{#if: UbuntuHelp:SwapFaq|日本語| [[::SwapFaq/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/ko | • {{#if: UbuntuHelp:SwapFaq|한국어| [[::SwapFaq/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/ksh | • {{#if: UbuntuHelp:SwapFaq|Ripoarisch| [[::SwapFaq/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/mr | • {{#if: UbuntuHelp:SwapFaq|मराठी| [[::SwapFaq/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/ms | • {{#if: UbuntuHelp:SwapFaq|Bahasa Melayu| [[::SwapFaq/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/nl | • {{#if: UbuntuHelp:SwapFaq|Nederlands| [[::SwapFaq/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/no | • {{#if: UbuntuHelp:SwapFaq|norsk| [[::SwapFaq/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/oc | • {{#if: UbuntuHelp:SwapFaq|occitan| [[::SwapFaq/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/pl | • {{#if: UbuntuHelp:SwapFaq|polski| [[::SwapFaq/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/pt | • {{#if: UbuntuHelp:SwapFaq|português| [[::SwapFaq/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/ro | • {{#if: UbuntuHelp:SwapFaq|română| [[::SwapFaq/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/ru | • {{#if: UbuntuHelp:SwapFaq|русский| [[::SwapFaq/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/si | • {{#if: UbuntuHelp:SwapFaq|සිංහල| [[::SwapFaq/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/sq | • {{#if: UbuntuHelp:SwapFaq|shqip| [[::SwapFaq/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/sr | • {{#if: UbuntuHelp:SwapFaq|српски / srpski| [[::SwapFaq/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/sv | • {{#if: UbuntuHelp:SwapFaq|svenska| [[::SwapFaq/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/th | • {{#if: UbuntuHelp:SwapFaq|ไทย| [[::SwapFaq/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/tr | • {{#if: UbuntuHelp:SwapFaq|Türkçe| [[::SwapFaq/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/vi | • {{#if: UbuntuHelp:SwapFaq|Tiếng Việt| [[::SwapFaq/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/yue | • {{#if: UbuntuHelp:SwapFaq|粵語| [[::SwapFaq/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/zh | • {{#if: UbuntuHelp:SwapFaq|中文| [[::SwapFaq/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/zh-hans | • {{#if: UbuntuHelp:SwapFaq|中文(简体)| [[::SwapFaq/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:SwapFaq | UbuntuHelp:SwapFaq | {{#if: | :}}SwapFaq}}/zh-hant | • {{#if: UbuntuHelp:SwapFaq|中文(繁體)| [[::SwapFaq/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:SwapFaq|:SwapFaq|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :SwapFaq/zh | | {{#ifexist: SwapFaq/zh | | {{#ifeq: {{#titleparts:SwapFaq|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:SwapFaq|1|-1|}} | zh | | }}
- title Swap FAQ
Introduction
- This FAQ is aimed at Linux novices.
- People always wonder how much swap they should create at install time, or after installing they may think, "have I made a large enough swap? Should I reinstall with a larger swap?"
- This FAQ will tell you how much swap you need and how to add more swap after installation.
- You will be given very simple answers (to prevent losing too much time reading this FAQ) and some explanations that may help you form your own opinion. For a more detailed explanation of memory and swap, see here.
What is swap?
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.
Why do I need swap?
Memory consuming programs Sometimes, a large program (like OpenOffice, Neverwinter Nights, or a video editor) make the entire system need extra memory. A significant number of the pages used by these large programs during its startup may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other programs or even for the disk cache. In these cases, swap will be used to help the system handle any extra load. Hibernation (suspend-to-disk) The hibernation feature (suspend-to-disk) writes out the contents of RAM to the swap partition before turning off the machine. Therefore, your swap partition should be at least as big as your RAM size. The hibernation implementation currently used in Ubuntu, swsusp, needs a swap or suspend partition. It cannot use a swap file on an active file system. 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.
How do I add more swap?
<<Anchor(addswap)>> 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.
Four-step Process to Add Swap
- 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.
For Adding a 512 MB swap
- Creating a file for 512 MB size you want:
We will create a /mnt/512Mb.swap swap file.
sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512
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.
- Formatting that file to create a swapping device:
sudo mkswap /mnt/512Mb.swap
- Adding the swap to the running system:
sudo swapon /mnt/512Mb.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/512Mb.swap none swap sw 0 0
Save and reboot.
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 dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512 Password: 512+0 records in 512+0 records out 536870912 bytes (537 MB) copied, 35.3802 seconds, 15.2 MB/s user@computer:~$ sudo mkswap /mnt/512Mb.swap Setting up swapspace version 1, size = 536866 kB no label, UUID=dd6a01c8-93f0-41e0-9b7a-306956d8821b user@computer:~$ sudo swapon /mnt/512Mb.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 user@computer:~$ free total used free shared buffers cached Mem: 499496 492768 6728 0 1240 142336 -/+ buffers/cache: 349192 150304 Swap: 524280 53384 470896 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/512Mb.swap none swap sw 0 0
Remove the swap from the running system and remove the swap file.
sudo swapoff /mnt/512Mb.swap && sudo rm /mnt/512Mb.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?
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 [[UbuntuHelp:[addswap|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/sbin:
sudo gedit /usr/sbin/swap2ram.sh
- copy&paste the script into the file:
#!/bin/sh 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 swap2ram.sh
- execute:
sudo swap2ram.sh