Virtual Memory – The Swap Problem

In my quest for the most efficient server environment that I can create I have spent a good portion of my life drilling down to the lowest levels of the linux kernel.  This helps to me better understand how to make use of and in some cases limit cpu and I/O usage to make the server perform well.  One very important aspect of server performance is virtual memory.

Virtual memory is memory is that stored on a disk of some sort that acts as main system memory.  So if your machine has 16 gigs of memory, and you have 16 gigs of swap space (virtual memory), then your server can operate as if it has 32 gigs of main system memory.  Virtual memory/swap space has been used for decades as a way for machines to accomplish more than what they were originally built to handle, and for the most part its worked well.  The problem is that it is SLOW.  A traditional spindle/platter based disk has access times (Think flipping through a phone book to look up a name) that is anywhere from 700x to 2500x slower than normal system memory.  Thats a problem if you have a server that uses swap a lot.

There is a lot more to the swap debate than what I mention above, and there are many articles written that could explain its benefits and detriments much better than I ever could.  I am writing about swap today to outline some changes that I think would greatly benefit how swap works in a multiuser environment (Shared hosting).

Since swap capabilities have generally not changed in 25 years it would be nice if someone could take a 2nd look at the feature set and update with features to benefit todays users.  Here are a few of my proposals.

1) Ability to swap based on a specific uid or gid (user id, or group id).  On a shared user platform some users dominate the memory usage.  While you can control memory usage for a certain user you can’t control the swappiness of certain users.  It would be great for “greedy” memory users to still be able to use swap, but have those specific users also endure the speed penalties that increased memory usage requires.  This would be GREAT to have!

2) Ability to swap by pid/tid (Process ID, and thread ID) – Some programs lend themselves to swapping while other don’t.  If I could specify certain apps to swap more aggressively than others I could fine tune the system to work better.  I know better than the kernel the performance I need from each application.  Let me use that knowledge to tweak virtual memory usage by pid/tid.

3) Ability to NOT swap by pid/tid – Just as I  stated above that I need to be able to increase the swappiness of certain applications, I also need the ability to exclude certain apps from swapping unless it is absolutely necessary.  MySQL is a good example of this.  I don’t want it swapping at all.  Not even a little bit.  I sure wish I could turn it off for this app!

4) Time based reporting for swap usage.  Everyone knows that swap usage is slow, but HOW SLOW?  What is the average time in milliseconds to swap out and swap in memory for the app you are using?  How many milliseconds per page in the last 1,5,15,30 seconds etc.  How overloaded or underloaded is the swap device?  Are the seeks killing the swap device?  Time to switch to an SSD for swap? How many pages are being swapped out or in for a specific app or uid.

5) Swap priorities for devices based on performance.  You can specify multiple swap partitions/files if you like currently.  However you have to set the priority for those devices when swap is activated.  Swap out and swap in functions should be able to be set based on response time of swap devices.  If a single swap device is getting crushed, but you have multiple devices why not use the other one to mitigate the slowness of the first swap device.  This is NOT as simple as round robin type load balancing of swap devices.  It needs to be intelligent and use devices based on response time in milliseconds.  It should also be able to split pages for specific apps across multiple swap devices (If you desire) so that swap in is faster (Similar to raid 5 setup).

Swap algorithms are too simple for a time that is long gone in todays computing environment.  Get rid of all the global settings, and give us some fine tuned options to make swap perform at the level we need it to be.  If there are any coding Santa Clauses out there please put an updated virtual memory manager in my virtual stocking!

If you think I am wrong on these swap issues please send your replies to “” :)

Matt Heaton / President

7 Responses to “Virtual Memory – The Swap Problem”

  1. charles says:

    a flexible size virtual memory might be useful too. why there must be a limit? although its not always require 16gb, but sometime its insufficient. certain IO process take up lots of vm space, it cant be completed without sufficient space.
    a level 2 virtual memory maybe!

  2. Don says:

    I think charles has a point there

  3. Russ says:

    I agree that swap algorithms are too basic and that this technology needs advancing. It seems sort of archaic the way it works right now. Its all about customization and speed based on a users requirements.

  4. Kim says:

    I never know how to alter the virtual memory of my computer so that it runs with better performance. And I am sure their are tricks within the server environment as well on tuning to maximum performance. It seems to me that swapping memory isnt too efficient in todays computing environment and that a breaktrhough is needed in this area.

  5. bluehost says:

    Two indicators of a RAM shortage are the scan rate and swap device activity.

  6. Ken Baker says:

    Hi Matt, just read part of blog for first time. With most recent re Disney cruise. My comment is that I appreciate the feeling of communicating with your clients in a sincere way. I personally have so much to learn about the web business, but I will say the attributes of the site I purchased seem to be the best I have seen in the Industry.

    Ken baker

  7. hostgator says:

    swap algorithms are too basic and need advancement. Hoping 2010 is monumental in this arena. A focus on memory will be crucial as the web continues to explode… Hosting has made great advancements in the first decade of 2000′s.

Leave a Reply