In my last post I discussed a few advantages of using virtual machines for development. This post deals with a huge downside – memory usage.
When you run a virtual machine you’re running two operating systems (or more) on your hardware – the guest and the host. Both require resources just to be active. If your hardware doesn’t have enough memory to run both operating systems, you’re in for a world of hurt as the host will be constantly paging to try and keep up.
There are a couple of ways to mitigate this problem. Obviously you can just go and buy more RAM. More memory for the host means that you can allocate more to the guest. Not a big deal if your hardware is new and you can still get memory for it. The next obvious thing is to run your host as lean as possible. Close your email, your music player, twitter, etc to free up some memory. After this you should try and tidy up what you’re running on the guest. Only run the programs you absolutely need to be running in order to keep paging low.
One more thing you can do to trim your guest memory footprint is to avoid using the piece of the OS that takes up a large chunk of resources: the windowing system. How do you do this? With Linux, runlevel 3 is everything but the GUI, so that’s the first step. A quick search for how to do this with your distribution will bring up all kinds of resources. After you switch your runlevel your system will boot with a command prompt only.
From there you can use x-forwarding to do your work. In short, programs are run on the guest but the GUI is displayed on the host. Thus, there is no overhead on the remote machine for the window environment and you can use allocate less memory to the VM and get the same performance. (This isn’t limited to VMs; you can use XForwarding on any two computers connected to a network and have the appropriate software.)
There was no configuration needed on my setup. My guest is CentOS 6 and my host is OSX Lion. I booted the VM, then started a terminal on my Mac. In the terminal I typed the following:
$ ssh -X ryan@myvmname
This sets up an x-forwarding session. From there you can run your favourite windowed programs, such as emacs, vim, jEdit, add/remove programs, etc. I won’t go into any more detail here because there isn’t any; if your setup is different there will be resources online to help.
Using this setup we were able to cut the memory allocation of the guest in half with no change in performance. It’s a pretty simple way to get the advantages of all Linux has to offer with the convenience of your favourite desktop.