个人工具

UbuntuHelp:SwapFaq

来自Ubuntu中文

Oneleaf讨论 | 贡献2007年5月24日 (四) 14:46的版本 (新页面: {{From|https://help.ubuntu.com/community/SwapFaq}} {{Languages|UbuntuHelp:SwapFaq}} #title Swap Partition FAQ === Introduction === **** '''This faq is aimed at Linux novices.''' **** P...)

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航, 搜索
  1. title Swap Partition FAQ

Introduction

        • 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 "oh my god", have I put enough swap? Maybe I should just reinstall with more swap?
        • This faq will tell you how much swap you need and how to add more swap after install.
        • 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.


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 disk big enough, just put 2*n Mb swap.

What is swap for?

        • As we'll see, there are basically four different purposes for swap :
  • Some programs really are memory-consuming.
  • Extra memory might come in handy.
  • Optimizing memory usage.
  • Hibernation (suspend-to-disk)
        • To begin with, let's say 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.
  • Nowadays, ram are often big enough so that we could use the computer without any swap at all.
        • Some programs really are memory-consuming :
  • Sometimes, something big (like openoffice, neverwinter nights or some 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.
        • Extra memory might come in handy :
  • 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).
  • In these cases, swap will give you an extra delay to figure out what happens or to finish something.
        • Swap can optimize memory usage :
  • Hard drives are considerably slower than ram. So when you need a file (be it a data file, like this video you're watching againg and again, executables, like firefox, or libraries), linux 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 more faster (thousands of times faster). We call "cached memory" these portions of ram that accelerate disk read. They make a huge difference in terms of responsiveness.
  • Linux automagically 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.
        • Hibernation needs swap
  • 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.

Should I reinstall with more swap?

        • Definitely no.
        • If you need more swap, just read the answer to "How do I add more swap ?".

How do I add more swap?

        • Usually, people associate swap with 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 conventionnal file.
        • It's true that swapping to a real partition is faster than swapping to a file, but not that faster. If you're considering responsiveness, my advice: 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.
  • The command :
  • sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512
  • 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.
        • 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 additionnal swap is now available.
        • d- Making the change permanent :
  • Add this line to your /etc/fstab :
  • /mnt/512Mb.swap none swap sw 0 0

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 isuue 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 isue 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 as to how to do it) or create a swap partition. In order to do that, you can for example

        • 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
        • or just use cfdisk (lookup its man page for more info).

However, let us just 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 and then enable it again
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


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

Author

About the document

        • TODO :
  • Determine the more standard emplacement for swap files (FHS).
  • Add power suspend considerations.
        • 15/05/2005, 01:42
  • First version.