[fedora-virt] swap space on virtual machines

Dor Laor dlaor at redhat.com
Sun Jul 26 14:22:48 UTC 2009

On 07/26/2009 03:48 PM, Rich Mahn wrote:
> Dor Laor wrote:
>> On 07/25/2009 09:02 PM, Richard W.M. Jones wrote:
>>> On Fri, Jul 24, 2009 at 12:39:54PM -0400, Rich Mahn wrote:
>>>> How big should the swap parititons be on virtual machines
>>>> under qemu, qemu/kvm?
>>>> It seems to me that if the VM actually needs swap space, it
>>>> would be more efficient to allocate more virual memory to it.
>>>> I know it's possible to allocate more virtual
>>>> memory than there is physical memory.  Intuitively (which
>>>> isn't always right) it would seem that allocating more
>>>> memory is more efficient than letting the VM use swap space.
>>>> It would seem that the host machine would have to either use
>>>> physical memory or swap space if it's required.
>>> A real interesting question.
>>> One place I think you're wrong is the assumption that adding more
>>> memory to a VM is better than having the VM use a swap disk.  The
>>> reason would be that the VM's memory manager will assume that the
>>> [from its point of view] physical memory will be much faster than
>>> swap, and so will arrange memory vs swap use accordingly.  But this
>>> assumption isn't true, this so-called physical memory is really just
>>> as slow as swap!
>>> You say:
>>>> Compared to the VM using a swap space that is a virtual disk space
>>>> that goes to real disk.
>>> but actually virtualized block I/O isn't so bad if you use the correct
>>> (virtio) drivers.
>> I think several terms are mixed together here:
>> The guest is given certain amount of ram to play with.
>> Various hypervisors virtualize this guest ram differently. Regardless of
>> the hypervisor. The guest does assumes it is his ram.
>> Even if you allocate large amount of ram to the guest, it might swap
>> since the guest runs processes that uses virtual memory, and thus might
>> exceed the allocated ram to the guest. Guest swapping is a reasonable
>> scenario that should be allowed and supported.
>> When using a fast virtual block device like virtio, it should even be
>> almost as fast as native swapping (which is always slower then ram..).
>> KVM hypervisor runs a VM as standard process. As such, it allocate
>> virtual userspace memory to the VM as ram. We can overcommit the host
>> ram and even swap it from the host.
>> Host swapping works nicely but might really slow down things since both
>> host&  guest will swap out/in/out the same pages.
>>> I think the only way you'll really answer this is to conduct tests
>>> based on your specific workload.  I've only done this with Xen, where
>>> this doesn't apply because Xen guest memory _is_ physical memory.
>>> By the way, Kernel Shared Memory (KSM, [1]) complicates everything...
>> But make things better ;)
>>> Rich.
>>> [1] http://lwn.net/Articles/306704/
>> So bottom line, if you have free ram, give it to the guest - why save
>> it? Even large guest can swap, determined by the load.
>> Dor
> Okay, my understanding of how RAM is used in Linux is limited.  If a
> system has, say 1G of RAM and runs fine without swapping, doesn't that
> same system usually run better with 2G of RAM?  In other words, doesn't
> the OS sprawl out to use all available RAM, with disk cache, or some

It depends on the load.
If the VM is idle, then it won't have any effect.
If the VM does not need allot of memory, the same.
If the VM swaps, sure it will help.

> such other items?  If that's the case, then it's important not to
> allocate more RAM to VMs than is actually available.  OTOH, if the OS

Again, depending on the host load. You can overcommit VM memory and it 
might be good if you have many VMs that have low memory foot print.
If it is not the case, you better not do it.

> doesn't sprawl out and only uses the same amount of memory (assuming no
> swapping is needed) regardless of the amount of physical memory, then
> oversubscribing the RAM allocated to VMs shouldn't be a problem if most
> of the time they don't use it.
> While I'm asking questions, I know that I can use 'swapon -s' to get the
> current swap situation.  Is there a command I can use to get more
> information on swap usage, such as high watermark, average usage, or
> some such data?

You can use vmstat 1 to track the io activity. Even tweaking the sysctl 
is possible.

> Thanks,
> Rich
> ------------------------------------------------------------------------
> No virus found in this outgoing message.
> Checked by AVG - www.avg.com
> Version: 8.5.392 / Virus Database: 270.13.30/2263 - Release Date: 07/26/09 06:33:00
> ------------------------------------------------------------------------
> _______________________________________________
> Fedora-virt mailing list
> Fedora-virt at redhat.com
> https://www.redhat.com/mailman/listinfo/fedora-virt

More information about the Fedora-virt mailing list