Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2007
    Location
    /dev/zero
    Posts
    143

    Swap memory on Xen VPS

    Hello,
    I have a low-end XEN VPS (created with Citrix Xen Technology), that's already had a swap partition [256MB]:
    $ fdisk -l
    Device Boot Start End Blocks Id System
    /dev/xvda1 * 1 1244 9992398+ 83 Linux
    /dev/xvda2 1245 1305 489982+ 5 Extended
    /dev/xvda5 1245 1305 489951 82 Linux swap / Solaris
    $ blkid
    /dev/xvda1: UUID="09c18828-2de7-4c6d-9f34-37aa8c6bd28c" TYPE="ext3"
    /dev/xvda5: TYPE="swap"
    However, I have created a swap file and enabled it to work together with swap partition:
    $ dd if=/dev/zero of=/swapfile bs=1024 count=262144
    $ mkswap /swapfile
    $ swapoff -a
    $ swapon /dev/xvda5
    $ swapon /swapfile
    $ nano /etc/fstab
    and append this line:
    /swapfile swap swap defaults 0 0
    Then use
    $ free -m
    to see the total swap memory = swap partition + swap file memory correctly.

    My question is:
    Will it occurs any problem while using both of 2 types of swap memories?
    Thanks for sharing your experiences about this question!
    Last edited by lovelycesar; 11-11-2010 at 08:33 AM.
    vpsoffers.com - a modest place for vps offers by sim so dep

  2. #2
    Join Date
    Mar 2009
    Location
    Austin, TX
    Posts
    935
    No, you won't have problems.

    However, if you are using swap (regardless of what virtualization technology used), then you have a more serious problem on hand. Either 1) optimize what you can 2) Reduce the number of apps running 3) upgrade your VPS. Swap is already bad as is on physical servers. When you know you are sharing disk I/O with others, it just makes the problem exponentially worse. With someone using swap, it just lags for everyone else on the node.

  3. #3
    Join Date
    Jul 2007
    Location
    /dev/zero
    Posts
    143
    thanks, but i could not understand all you said. please explain things.
    vpsoffers.com - a modest place for vps offers by sim so dep

  4. #4
    In a nutshell -
    1. Using your swap file is OK
    2. Don't try to solve the performance issue by creating another swap file (if this is the case)
    CloudVPS
    Affordable VPS Servers - Middle East
    High performance | Green technologies

  5. #5
    Join Date
    Sep 2004
    Posts
    1,007
    You probably don't want anything more than the existing 256MB swap partition. If you need more swap than that, you're probably going to have serious performance concerns.

  6. #6
    If you need a swap file for light usage, it can be ok, but generally, swap is a really bad idea on a VPS. We only allow customers to use swap if they put the swap on an SSD or if they have a dedicated hard drive, because it is very easy for swap usage to kill performance for all of your customers at once, just because one person's machine starts using swap. So I would be very careful with it.
    IOFLOOD.com -- We Love Servers
    Phoenix, AZ Dedicated Servers in under an hour
    ★ Ryzen 9: 7950x3D ★ Dual E5-2680v4 Xeon ★
    Contact Us: sales@ioflood.com

  7. #7
    Join Date
    Jul 2007
    Location
    /dev/zero
    Posts
    143
    But the box I mentioned is based on a Xen Server, it means the box's using dedicated/private resources. In my case and in theory, if the usage of memory exceeds 128MB, then swap memory will be used; so that performance of whole box will not be influenced by an over-usage of memory; that's right?
    I see on the Xen VPS's market, they provide always packages coming with dedicated/swap memories.
    I'm more and more being confused by this topic.
    Thanks for all of your opinions.

    @funkywizard: Of course, I use my low-end box for a light usage, but having a full-installation of LAMP server.
    @all: for details, I post here memory infos with free command:
    Code:
    ~# free -m -t (swap partition is activated by default)
                 total       used       free     shared    buffers     cached
    Mem:           142         77         64          0          4         37
    -/+ buffers/cache:         35        107
    Swap:          478          0        478
    Total:         620         77        543
    and after activating swap file memory:

    Code:
    ~# free -m -t
                 total       used       free     shared    buffers     cached
    Mem:           142         77         64          0          4         37
    -/+ buffers/cache:         35        107
    Swap:          882          0        882
    Total:        1024         77        947
    Last edited by lovelycesar; 11-11-2010 at 07:26 PM.
    vpsoffers.com - a modest place for vps offers by sim so dep

  8. #8
    Join Date
    Sep 2004
    Posts
    1,007
    Disk IO is still shared on a Xen VM, and swap is hitting the disk. Swap is super slow, and as such should be avoided. You want a little bit so that inactive stuff can be swapped out of RAM, but you don't want anything important to get into swap.

    In your case, you have 142MB of RAM. I would suggest something like 71MB of swap memory. You have enabled 882MB.

    Let me try to explain swap this way. You have two types of memory on your machine (using typical values):

    Primary Memory (RAM)
    Access latency: 0.000009 ms
    Access speed (sustained/random): 10667 MB/s (DDR3-1333)

    Secondary Memory (HDD: swap)
    Access latency: 15 ms
    Access speed (sustained reads): ~100 MB/s
    Access speed (random reads): ~2 MB/s

    So, the hard disk is a lot slower than RAM. RAM can transfer data 5000 times faster, and it can access data with one million times less latency. RAM is fast, disks are slow.

    Your system can't execute stuff out of swap. It has to be in active RAM to execute. So data in the swap is inactive; it's been swapped out of active memory for long-term storage.

    Now, normally this is beneficial if you don't *NEED* the stuff that's being swapped out of active memory. But as soon as you run out of RAM and actually start needing to swap active stuff out of memory, the system slows to a crawl. Relying on swap in such a scenario, the system will appear to be more or less frozen due to how much slower the disk is.

    As such, you want to have just enough swap memory to be useful to get unneeded stuff out of RAM, but not enough swap that you regularly start relying on it.

    Instead, you should be configuring your software to work in the limited amount of memory that you have. You don't have much to work with, so you need to do a few things:

    1) Either severely limit how many child processes Apache can spawn, or switch to a web server known for being memory-light (like lighttpd, nginx, etc). I find that fastcgi can help, because it lets you control the number of dynamic (PHP) and static (images, HTML, etc) processes directly. The default Apache configuration is designed for a system with like 16GB of RAM. You don't have 16GB of RAM

    2) Make sure you're using the appropriate MySQL configuration, or perhaps don't use MySQL at all. You'll want to be using the my-small.cnf sample configuration, which is designed for machines where you can dedicate less than 64MB of RAM to the database. Performance will suffer, but you need more RAM to get more performance at that point.

    3) Shut down and/or uninstall services that you don't need. It adds up.

  9. #9
    Join Date
    Aug 2010
    Posts
    239
    Swap "memory" is really space on your hard-disk and not in RAM. RAM is multiple times faster than hard-disk. I believe that you are thinking that swap = space in ram, when swap = space on your disk!

    Guspaz has detailed everything.

  10. #10
    I definitely agree with Guspaz. Ram is 1000's of times faster than the hard drive, so generally you want to avoid using swap memory. It's ok if programs that aren't actually being used get swapped out of ram, freeing up ram for other things. But if you're using more than a little bit of swap memory, then certainly a lot of *active* things are getting pushed into and out of swap (the hard drive) constantly. This will bring the entire server to it's knees rather quickly if the swap file is stored on a normal hard drive.
    IOFLOOD.com -- We Love Servers
    Phoenix, AZ Dedicated Servers in under an hour
    ★ Ryzen 9: 7950x3D ★ Dual E5-2680v4 Xeon ★
    Contact Us: sales@ioflood.com

  11. #11
    Join Date
    Jul 2007
    Location
    /dev/zero
    Posts
    143
    Thank for all of your opinions.
    Before starting this thread, I have configured Apache for a lowend resource as:
    mpm-prefork: start 1 process
    Max Requests Per Child: 120 - Keep Alive: on - Max Per Connection: 90
    Timeouts Connection: 60 - Keep-Alive: 15
    either
    skip-bdb
    skip-innodb
    in my.cnf of MySQL.

    I knew that : RAM > SWAP PARTITION > SWAP FILE in order of better performance, but why did people introduce swap? and what is it for?

    Via all of your feedbacks, could I have a conclusion: It's not necessary to have swap memory. Is that right?
    Last edited by lovelycesar; 11-13-2010 at 03:01 AM.
    vpsoffers.com - a modest place for vps offers by sim so dep

  12. #12
    You probably need some swap memory, especially if you have very little real memory. In a dedicated server environment, it's a little less harmful. If you do something wrong, and end up using too much virtual memory, and your server crashes, with a dedicated server, it only affects you. With a VPS, if you use too much virtual memory, you crash everyone else too.
    IOFLOOD.com -- We Love Servers
    Phoenix, AZ Dedicated Servers in under an hour
    ★ Ryzen 9: 7950x3D ★ Dual E5-2680v4 Xeon ★
    Contact Us: sales@ioflood.com

  13. #13
    Join Date
    Jul 2007
    Location
    /dev/zero
    Posts
    143
    Thanks all,
    I'm on Xen, so I think this does not affect other VPS.
    I have removed Swap file, just kept the default Swap partition.
    Skip Berkeley DB and InnoDB for MySQL.
    For Apache with mpm-prefork:
    KeepAlive On
    MaxKeepAliveRequests 25
    KeepAliveTimeout 10
    <IfModule mpm_prefork_module>
    StartServers 1
    MinSpareServers 1
    MaxSpareServers 2
    MaxClients 25
    MaxRequestsPerChild 50
    </IfModule>
    This take about under 80 MB of RAM, for running one CMS site.
    vpsoffers.com - a modest place for vps offers by sim so dep

  14. #14
    Join Date
    Mar 2005
    Location
    Labrador, Canada
    Posts
    988
    Quote Originally Posted by lovelycesar View Post
    StartServers 1
    MinSpareServers 1
    MaxSpareServers 2
    I'd increase those slightly, otherwise Apache is going to spend a lot of time spinning up and killing off child servers. And your page load times will be less than optimum as a result.

    Maybe something like this for a start...

    StartServers 3
    MinSpareServers 2
    MaxSpareServers 4

Similar Threads

  1. Replies: 4
    Last Post: 10-02-2010, 05:07 PM
  2. Replies: 0
    Last Post: 10-02-2010, 04:34 AM
  3. Replies: 2
    Last Post: 10-02-2007, 02:21 AM
  4. Replies: 2
    Last Post: 08-02-2007, 12:05 AM
  5. Replies: 7
    Last Post: 07-21-2007, 05:11 PM

Tags for this Thread

Posting Permissions

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