个人工具

“UbuntuHelp:SwapFaq”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第4行: 第4行:
 
=== Introduction ===
 
=== Introduction ===
 
* '''This FAQ is aimed at Linux novices.'''
 
* '''This FAQ is aimed at Linux novices.'''
* People always wonder how much swap they should put on install, or after installing without a clue think, have I put enough swap? Maybe I should just reinstall with more swap?
+
* 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.
 
* 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 (so that you do not have to lose too much time reading this FAQ) and some explanations that may help you make your own opinion.
+
* 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].
 
=== How much swap do I need? ===
 
=== 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 n MB of RAM, you need between n and 2*n MB of swap.
* If you have a disk big enough, just put 2*n MB swap.
+
* If you have a large enough disk, use 2*n MB swap.
 
=== What is swap for? ===
 
=== What is swap for? ===
* As we'll see, there are basically four different purposes for swap :
+
* As we'll see, there are basically four different purposes for swap:
* Some programs really are memory-consuming.
+
* Some programs are very memory-consuming.
* Extra memory might come in handy.
+
* Extra memory can come in handy.
 
* Optimizing memory usage.
 
* Optimizing memory usage.
 
* Hibernation (suspend-to-disk)
 
* Hibernation (suspend-to-disk)
* To begin with, let's say that computers have changed a lot since swap was first used :
+
* 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. You would use swap so that the available memory would be the addition of the RAM space and the swap space.
+
* 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 are often big enough so that we could use the computer without any swap at all.
+
* Nowadays, RAM is often big enough that computers may not require swap.
* <u>Some programs really are memory-consuming :</u>
+
* <u>Some programs are very memory-consuming:</u>
* Sometimes, something big (like OpenOffice, Neverwinter Nights or some video editor) make the entire system need extra memory.
+
* 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 make the system able to handle the extra load.
+
* In these cases, swap will be used to help the system handle any extra load.
* <u>Extra memory might come in handy :</u>
+
* <u>Extra memory might come in handy:</u>
* Unforeseeable events, might and will happen (a program going crazy, some action needing much more space than you thought, or any other unbelievable combination of events).
+
* 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 will give you an extra delay to figure out what happens or to finish something.
+
* In these cases, swap can give you an extra delay to figure out what happened, or to finish what you are working on.
* <u>Swap can optimize memory usage :</u>
+
* <u>Swap can optimize memory usage:</u>
* Hard drives are considerably slower than RAM. So when you need a file (be it a data file, like this video you're watching again and again, executables, like Firefox, or libraries), the Linux kernel reads the file into RAM and keeps it there so than the next time you need it again, it's already in RAM and data access is much faster (thousands of times faster). We call these portions of RAM that accelerate disk read "cached memory." They make a <u>huge</u> difference in terms of responsiveness.
+
* 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 <u>huge</u> difference in terms of responsiveness.
* The Linux kernel automatically moves RAM reserved by programs but not really used in swap so that this RAM can serve the better purpose of having more cached memory.
+
* 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.
 
* <u>Hibernation needs swap</u>
 
* <u>Hibernation needs swap</u>
* The hibernation feature (suspend-to-disk) writes out the contents of the memory 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, and can not use a swap file on an active file system.
+
* 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? ===
 
=== Should I reinstall with more swap? ===
* Definitely no.
+
* 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]).
 
* 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]).
 
=== How do I add more swap? ===
 
=== How do I add more swap? ===
* Usually, people associate swap with a swap partition, maybe because they've been proposed to create a swap partition on install. 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: add more RAM. Swapping to a partition or a file won't change anything.
+
* 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.
 
* We will add more swap by adding a swap file.
 
* <u>Adding more swap is a four-step process :</u>
 
* <u>Adding more swap is a four-step process :</u>
第42行: 第42行:
 
* d- Making the change permanent.
 
* d- Making the change permanent.
 
* <u>We will consider (as an example) a 512 Mb swap need.</u>
 
* <u>We will consider (as an example) a 512 Mb swap need.</u>
* <u>a- Creating a file the size you want :</u>
+
* <u>a- Creating a file the size you want:</u>
 
* We will create a /mnt/512Mb.swap swap file.<pre><nowiki>
 
* 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>
* What is important here is '''count=512''', which means we want our file to contain 512 blocks of '''bs=1M''', which means block size = 1 MegaBytes.
+
* 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'''''
 
* 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.
+
Though the file grows to 512Mb immediately, it will have holes that makes it unusable.
* <u>b- Formatting that file to create a swapping device :</u><pre><nowiki>
+
* <u>b- Formatting that file to create a swapping device:</u><pre><nowiki>
 
sudo mkswap /mnt/512Mb.swap
 
sudo mkswap /mnt/512Mb.swap
 
</nowiki></pre>
 
</nowiki></pre>
* <u>c- Adding the swap to the running system :</u><pre><nowiki>
+
* <u>c- Adding the swap to the running system:</u><pre><nowiki>
 
sudo swapon /mnt/512Mb.swap
 
sudo swapon /mnt/512Mb.swap
 
</nowiki></pre>
 
</nowiki></pre>
* You can see with "'''cat /proc/meminfo'''" that your additionnal swap is now available.
+
* You can see with "'''cat /proc/meminfo'''" that your additional swap is now available.
* <u>d- Making the change permanent :</u>
+
* <u>d- Making the change permanent:</u>
 
* edit your /etc/fstab: <pre><nowiki>
 
* edit your /etc/fstab: <pre><nowiki>
 
gksudo gedit /etc/fstab
 
gksudo gedit /etc/fstab
第128行: 第128行:
 
</nowiki></pre>
 
</nowiki></pre>
 
=== Performance tuning with ''swappiness'' ===
 
=== Performance tuning with ''swappiness'' ===
The ''swappiness'' parameter controls the tendency of the kernel to move processes out of physical memory and on to the swap disk.  As 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 ''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'' 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=0'' tells the kernel to avoid swapping processes out of physical memory for as long as possible
第163行: 第163行:
 
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 (see above as to how to do it) or create a swap partition. In order to do that, you can for example
+
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 usin the expert bot option), create a swap partiton out of the free space on your hardddisk and then interrupt your installation
+
* 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).
 
* or just use cfdisk (lookup its man page for more info).
However, let us just suppose you have a swap partition.
+
However, let us suppose you have a swap partition.
 
There are several ways of enabling it.
 
There are several ways of enabling it.
 
* 2.1- First make sure you have something like this <pre><nowiki>
 
* 2.1- First make sure you have something like this <pre><nowiki>
第175行: 第175行:
 
</nowiki></pre>
 
</nowiki></pre>
 
in /etc/fstab. It enables swap on boot.
 
in /etc/fstab. It enables swap on boot.
* 2.2- Then try to disable all swap, recreate it and then enable it again <pre><nowiki>
+
* 2.2- Then try to disable all swap, recreate it, then re-enable it <pre><nowiki>
 
swapoff -a
 
swapoff -a
 
/sbin/mkswap /dev/hda8
 
/sbin/mkswap /dev/hda8
第181行: 第181行:
 
</nowiki></pre>
 
</nowiki></pre>
 
* 2.3 Some people also reported this to help:
 
* 2.3 Some people also reported this to help:
* 1) cfdisk : erase the old Swap partition and recreate a new one
+
* 1) cfdisk : erase the old swap partition and recreate a new one
 
* 2) reboot (mandatory)
 
* 2) reboot (mandatory)
 
* 3) mkswap /dev/hda8
 
* 3) mkswap /dev/hda8
第193行: 第193行:
 
* Determine the more standard emplacement for swap files (FHS).
 
* Determine the more standard emplacement for swap files (FHS).
 
* Add power suspend considerations.
 
* Add power suspend considerations.
 +
* Add how to add the swap file to make Ubuntu hibernate to it
 
* 15/05/2005, 01:42
 
* 15/05/2005, 01:42
 
* First version.
 
* First version.
 
----
 
----
[[category:CategoryDocumentation]] [[category:CategoryFaq]] [[category:CategoryCleanup]]
+
[[category:CategoryFaq]] [[category:CategoryCleanup]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2009年5月12日 (二) 18:58的版本

  1. 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

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.