Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Please correct me if I am wrong. I assume VirtualBox, like any other application, is swapped to disk if it uses too much memory. Is there any way to force the system to always keep all the VirtualBox processes in memory?

share|improve this question

marked as duplicate by Michael Hampton, mgorven, mdpc, Khaled, Tom O'Connor Feb 14 '13 at 10:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

@mgorven: I really don't want swap at all for VirtualBox. Setting /proc/sys/vm/swappiness applies to the entire system and still swaps if the system is out of memory. I know I could do swapoff -a to avoid swapping, but that still applies to the entire system. I only want certain processes (VirtualBox). – f.ardelian Feb 13 '13 at 23:10

Is there any way to force the system to always keep all the VirtualBox processes in memory?

There's mlock(2) which "locks part or all of the calling process's virtual address space into RAM, preventing that memory from being paged to the swap area". In case you manage it to be called from VirtualBox, it would do, yep.

share|improve this answer
Except that mlock will not do anything for the guest OS should it (the guest OS) decide it needs to write pages to disk. All it will do is prevent the host from paging it out but not the guest from doing so. – Matt Feb 13 '13 at 23:20
@Matt, man page doesn't have any "except"s. It locks pages in RAM and that's all. – poige Feb 13 '13 at 23:37
@poige: Yes, you are correct. – f.ardelian Mar 10 '13 at 17:14

Give your host machine sufficient RAM for both the host OS and the virtual machine and set your swappiness value low to zero.

Inside the guest VM, turn off the swapfile. That might not seem so obvious, but it's the guest OS and it's applications that are making VirtualBox use RAM and it's the guest OS that will also page to disk (or virtual disk - which is rather slow).

The key is to have sufficient RAM for both the host and the guest vm.

share|improve this answer
Thanks, but this is not good enough. I do not want any swap at all. Setting swappiness to 0 only means the kernel will do its best to avoid swapping, but it will still swap if needed. "Sufficient RAM" is arbitrary and depends a lot on what other applications are running on the host. – f.ardelian Feb 13 '13 at 23:32

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