Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: How does the 'swap' drive actually work????

  1. #11
    Join Date
    Dec 2004
    Location
    Manchester
    Beans
    2,081
    Distro
    Ubuntu Studio 13.04 Raring Ringtail

    Re: How does the 'swap' drive actually work????

    Quote Originally Posted by Linux&Gsus View Post
    I never fully understood the need of a SWAP partition. It's needed when there is not enough RAM available. OK, I understand that. But what else is it really doing?
    I checked a server in the office the other day that used (at the end of the day) around 250-300MB RAM, no SWAP was used so far. The next morning RAM usage didn't change but a few MB SWAP was used. The system has 4GB RAM. Why was the SWAP used with still tons of RAM available?
    Quote Originally Posted by Linux&Gsus View Post

    On linux (and any other sane operating system) free ram is used as disk cache. The first time you read a file it takes a long time because you have to read it from the disk. When you read it a second time, it is already in the cache so it is fast. (this is why openoffice starts much faster the second time you open it).
    So the more free RAM you have the more disk cache you have.
    If you have a program loaded into memory, but you are not using it, it is still using up memory. Or a program may have leaked some memory, so it is allocated, but never accessed by that program. If the OS can move this memory onto the swap space, then there is more RAM free to do useful things with.
    Another question that I have is what happens when I suspend to disk. Is the SWAP partition used to save the data from the RAM? If so, what if there is not enough space available for RAM + potential SWAP usage?
    If not, where is the data saved to?
    Quote Originally Posted by Linux&Gsus View Post

    I have wonder about this. I imagine the hibernate must fail.
    Which then also leads me to the question, how can SWAP increase the performance of systems that has plenty of RAM (2GB+). Assuming that it's not a computer that is used for RAM intensive work like video editing, etc. I would guess that someone doing that has "enough" RAM according to his needs, anyways.
    So, with enough RAM, why wouldn't it be enough to have rather little SWAP, e.g. 300MB?
    it depends what you want to happen when a program goes crazy and grabs a vast amount of ram. when you run out of memory the kernel starts killing processes that it thinks might be responisble.

    If you have lots of swap, then there is a lower chance of getting to the out of memory case. however if the program is going to keep grabbing memory for ever, then you might do better to quickly hit out of memory, the kernel to kill stuff, and then reboot and get going again.

    Quote Originally Posted by Linux&Gsus View Post
    The last question that bugs me since a while is, since HDD's are rather slow, would it be possible to use a USB FlashDrive as SWAP partition? Would that increase the performance?
    hard disks still win for transfering large amounts of data. so it would depend on your usage pattern. if you get a 50MB/s flash disk, then it would probably beat most hard disks. a cheap pen drive will not be that fast.

  2. #12
    Join Date
    Jul 2006
    Beans
    2

    Re: How does the 'swap' drive actually work????

    Quote Originally Posted by Linux&Gsus View Post
    I got a question related to that as well.

    I never fully understood the need of a SWAP partition. It's needed when there is not enough RAM available. OK, I understand that. But what else is it really doing?
    Using swap space for insufficient memory is one reason for its existence, another is to have the capability to "swap" out memory that doesn't get used often (the latter reason is likely to take effect on longer running systems).

    I checked a server in the office the other day that used (at the end of the day) around 250-300MB RAM, no SWAP was used so far. The next morning RAM usage didn't change but a few MB SWAP was used. The system has 4GB RAM. Why was the SWAP used with still tons of RAM available?
    This is an example of swap space being used to purge infrequently used pages from physical RAM. You pointed out that it happened over night, so the system was doing something that caused it to notice that some pages weren't used at all and it swapped them out in favor of some other memory (probably related to the cache usage).

    BTW, did you notice how much of the 4GB of RAM was used including the cache? You have to think about cache usage as well when understanding the use of swap, not just resident memory (or virtual, etc, etc). Cache usage gets printed out with `free' as well. It's an important thing to make note of.

    Another question that I have is what happens when I suspend to disk. Is the SWAP partition used to save the data from the RAM? If so, what if there is not enough space available for RAM + potential SWAP usage?
    If not, where is the data saved to?
    Well, this is a case where you would want to have more swap space than you have memory (I think it would fail if you don't have enough space, but I'm not sure, never thought to try). IIRC, it is common to store the contents of physical memory in swap space when hibernating, but not necessary (it's configurable, but simpler to just use swap).

    Which then also leads me to the question, how can SWAP increase the performance of systems that has plenty of RAM (2GB+). Assuming that it's not a computer that is used for RAM intensive work like video editing, etc. I would guess that someone doing that has "enough" RAM according to his needs, anyways.
    To the kernel, there is no such thing as having "plenty of RAM". An operating system is only operating at peak performance when it is using all of physical RAM (minus a little bit for kernel emergencies, such as running out of memory). Even if you have enough RAM for your workload you will notice the rest of memory gradually creeps up to near 100% utilization because it is a waste of space not to use it (not to mention money), and performance can be improved by caching frequently used files (even if they aren't in use). Thus, the longer you run a system, the better it performs--well, it's supposed to anyway . This is also why more RAM can really help web servers and file servers (things that just dish out files) because more of those files can be cached, even if they don't really need to be cached, and even if you have plenty of RAM.

    (Want to see this phenomenon in action, execute a complex `find' command on a large amount of files. Then do it a second time. Time each run and take snapshots of memory utilization before and after. Try to rationalize what you see, if you can't then keep asking questions. Now repeat the experiment while also compiling a kernel. Still understand?)

    So not using swap space is OK if you have the memory, but it can degrade memory utilization by keeping pages resident that don't have to be resident at the cost of cache (or the like--it gets complex quick). BTW, this is the type of "performance" improvement that might be undetectable to users, especially if you don't know how to realize its effects throughout the system.

    The last question that bugs me since a while is, since HDD's are rather slow, would it be possible to use a USB FlashDrive as SWAP partition? Would that increase the performance?
    Flash drives can be used like any other file system, so I imagine they can be used for this purpose. In fact, I'm sure they are used for that purpose. Look at the laptops that are out there now! And yes, it would then be faster, but still much slower than RAM, and in the case of out-of-memory, you'd still be hurting in the same ways (disk swappiness, etc). Only RAM can replace RAM

  3. #13
    Join Date
    Apr 2008
    Beans
    297
    Distro
    Kubuntu 11.04 Natty Narwhal

    Re: How does the 'swap' drive actually work????

    Thanks guys for all the explanations. That helped a lot.
    I guess I understand the concept of caching files and that the RAM is used for that. I just wasn't too sure about all aspects of SWAP. E.g. I somehow would have expected that it would rather purge the cache than using SWAP.

    But now I feel enlightened. *Bows down*

    Cheers,
    Steve

  4. #14
    Join Date
    Dec 2004
    Location
    Manchester
    Beans
    2,081
    Distro
    Ubuntu Studio 13.04 Raring Ringtail

    Re: How does the 'swap' drive actually work????

    for got to mention. flash drives, have limitted write cycles. the problem is not as bad as it used to be.

    If you were hitting swap regularly, then the life og the flash driver may be reduced to something on the order of a few years. for a server that may not be acceptable. for a home system it should be fine.

  5. #15
    Join Date
    Apr 2008
    Beans
    297
    Distro
    Kubuntu 11.04 Natty Narwhal

    Re: How does the 'swap' drive actually work????

    Right the limited write cycles. But I never thought of the flash drives as a server solution, rather to re-use older ones for a desktop, instead of using up HD space.
    However, since the speed seems to not be as good as I thought it might not be the smartest idea.

    Cheers,
    Steve

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •