Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.
  • What is the purpose of a swap partition?
  • How do I know I have just enough swap partition? Not too much/little.

My Ubuntu PC is used for typical stuff:

  • Web (email, facebook, etc.)
  • Some movies
  • gaming is rare
share|improve this question
    
By what's been posted it seems I have WAY too much swap. 3GB RAM, 300GB HD, and a 8GB partition for swap. –  lamcro Oct 24 '10 at 2:29
1  
Extra swap that doesn't get used isn't going to hurt you, and reclaiming 1.7% of your drive to make it smaller is probably not worth the effort. Yes, it's far larger than you'll probably ever use, I wouldn't worry about it. –  msw Oct 24 '10 at 15:02
    
You can also see it as a "future proof" swap partition, if you ever add extra memory to your system, you won't have to enlarge the swap... :) –  JanC Oct 24 '10 at 15:29
add comment

4 Answers

The swap partition serves a couple of purposes.

  • It serves as 'backup' RAM. That is, should your computer run out of RAM, it will use the swap area as a temporary source of more memory. More specifically, it will 'swap' unused items from the RAM into the swap area in order to leave spare space for the applications that need it at that instant. This is not ideal as the data transfer rate to the hard drive is significantly lower than that to your normal RAM. In practice this means its much slower to retrieve information from the swap area.

  • It is used when the computer hibernates. Hibernation involves taking an image of the RAM in its current state (like an ISO represents an image), and saves it to the swap area. It then reloads this image when the computer restarts.

  • To be most useful, the swap area should be at least (RAM * 1.5) although more is recommended. For example, on my system with 3gb of RAM, I have a swap area of 7.2gb.

share|improve this answer
    
If you don't use something it is ideal to swap it to disk, and use the RAM for things that you do use (for example moving CUPS or other daemons you don't use very often to swap gives you more memory for caching disk data that actually gets used a lot). –  JanC Oct 24 '10 at 15:35
    
I doubt a hard drive image fits in the swap partition for hibernation. My hard drive is 500 GB and the swap is 12 GB. Perhaps you mean memory image. –  broiyan Oct 17 '11 at 1:44
add comment

This is very close to the same as this question about the “right” size for a swap partition. Much of the same information from my answer there applies - basically, if you want to hibernate you generally want your swap space to be at least as big as your RAM, and other than that a round number like 1 or 2 GB is easily sufficient. Because swap is so much slower than RAM, if you're filling up multiple gigabytes of swap your computer has almost certainly become unusably slow.

There's also no real need for a swap partition - swap files (available on the mainstream linux filesystems) give the same performance and make it trivially easy to add more swap space if you decide you haven't got enough.

share|improve this answer
add comment

here a very deep information about swap

some people say the double of your ram but personally i recommend this :

swap = 1.5 X Total Ram

Example :

if you have 2Gb of ram -> swap = 1.5 x 2 = 3

P.D : Ubuntu Desktop uses Swap to Hibernate (PC off, no power needed, program states saved). If Hibernation is important to you, have more swap space then ram + swap overflow.

share|improve this answer
add comment

The free command can tell you how much swap you are using. For example on this machine:

$ free -m
             total       used       free     buffers     cached
Mem:          1947       1863         84         312        758
-/+ buffers/cache:        792       1154
Swap:         4000          3       3997

shows me that I have 2GB (1947m) RAM and that the system has used most of it. However, 312m is used for I/O buffers and the remainder (758m) the system has decided to fill with disk cache.

The disk cache is interesting because it is using fast memory instead of slow disk for its contents. The contents could be gotten from disk, but they are kept around in case they are needed. This also means that there is 758m of memory that can be reclaimed in an instant if necessary because the system knows it can find that data on the disk instead.

That is why there is a second line showing that if there were no buffering and cache, I'd have half my RAM (1154m) available for use.

The third line shows that I have an overly large swap partition (it was there and wasn't doing anything) of which a whopping 3m have been used. This is stuff that the kernel really doesn't expect to have to use anytime soon so it was stuck out on the "back porch".

While free gives you the snapshot now, vmstat can give you a running picture:

$ vmstat 10
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  0   3588  86236 316524 769132    0    0    14    13  126   81  4  1 95  0
 0  0   3588  83872 316532 770512    0    0     0    20  264 1229  3  1 96  0

There's a lot of information there, but of interest is that there is no swap-in (si) or swap-out (so) traffic. Which means I'm not using the swap at all over the last 10 seconds.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.