特殊:Badtitle/NS100:SwapFaq
文章出处: |
{{#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.
How much swap do I need?
- If you have n MB of RAM, you need between n and 2*n MB of swap.
- If you have a large enough disk, use 2*n MB swap.
What is swap for?
- As we'll see, there are basically four different purposes for swap:
- Some programs are very memory-consuming.
- Extra memory can come in handy.
- Optimizing memory usage.
- Hibernation (suspend-to-disk)
- To begin, it's worth mentioning that computers have changed a lot since swap was first used:
- At first, swap was needed to extend real memory capacity. Users would utilize swap so that available memory would be the addition of both RAM space and swap space.
- Nowadays, RAM is often big enough that computers may not require swap.
- Some programs are very memory-consuming:
- Sometimes, a large program (like OpenOffice, Neverwinter Nights, or a video editor) make the entire system need extra memory.
- In these cases, swap will be used to help the system handle any extra load.
- Extra memory might come in handy:
- 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.
- Swap can optimize memory usage:
- Hard drives are considerably slower than RAM. So, 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's already in RAM and data access is much faster (thousands of times faster). The portions of RAM that accelerate disk read are called "cached memory." They make a huge difference in terms of responsiveness.
- The Linux kernel automatically moves RAM reserved by programs - but not really used in swap - so that it can serve the better purpose of extending cached memory.
- Hibernation needs swap
- 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.
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).
How do I add more swap?
- Usually, people associate swap with a swap partition, perhaps because they were asked to create a swap partition at install time. In fact, any file can be used as a swapping device, be it a partition or a conventional file. If you're considering responsiveness, my advice is to add more RAM. Swapping to a partition or a file won't change anything.
- We will add more swap by adding a swap file.
- Adding more swap is a four-step process :
- a- Creating a file the size you want.
- b- Formatting that file to create a swapping device.
- c- Adding the swap to the running system.
- d- Making the change permanent.
- We will consider (as an example) a 512 Mb swap need.
- a- Creating a file the 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
- What's important here is count=512, which means we want our file to contain 512 blocks of bs=1M, which means block size = 1 MegaBytes.
- 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.
- b- Formatting that file to create a swapping device:
sudo mkswap /mnt/512Mb.swap
- c- Adding the swap to the running system:
sudo swapon /mnt/512Mb.swap
- You can see with "cat /proc/meminfo" that your additional swap is now available.
- d- Making the change permanent:
- edit your /etc/fstab:
gksudo gedit /etc/fstab
- and 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
Priority of swap containers
- The Linux kernel assigns priorities to all swap containers. You can see them by
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
Performance tuning with swappiness
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
- Ubuntu uses a default setting of 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.
How to check the swappiness value
cat /proc/sys/vm/swappiness
How 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.
Troubleshooting
Note:this regards mainly swap on hard disk partitions, but it could help you anyway. Also replace /dev/hda8 with your configuration. Help! The 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
- 1- First try, if it is because the system cannot use swap or because it just does not need it. Start many memory consuming applications (e.g. Gimp, web browsers, OpenOffice etc) and then issue the free command again. Is swap being used now?
- 2- See if there is a swap partition at all:
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 (see above for instructions) or create a swap partition. In order to do that, you can:
- boot from your Ubuntu install CD (rather than using the expert bot option), create a swap partiton out of the free space on your hard disk and then interrupt your installation
- or just use cfdisk (lookup its man page for more info).
However, let us suppose you have a swap partition. There are several ways of enabling it.
- 2.1- First make sure you have something like this
/dev/hda8 none swap sw 0 0
in /etc/fstab. It enables swap on boot.
- 2.2- Then try to disable all swap, recreate it, then re-enable it
swapoff -a /sbin/mkswap /dev/hda8 swapon -a
- 2.3 Some people also reported this to help:
- 1) cfdisk : erase the old swap partition and recreate a new one
- 2) reboot (mandatory)
- 3) mkswap /dev/hda8
- 4) swapon -a
Author
- Yannick Le Saint (kyncani) <[email protected]>
- Tomas Hnyk <[email protected]> - troubleshooting section
- Max Randor <max (DOT) randor <at>googlemail.com>
About the document
- TODO :
- Determine the more standard emplacement for swap files (FHS).
- Add power suspend considerations.
- Add how to add the swap file to make Ubuntu hibernate to it
- 15/05/2005, 01:42
- First version.