« | Home | »

How Much Virtual Memory is Enough?

By Martin English | September 3, 2006

Ten years ago, Common Wisdom said that virtual memory should be, on the average, two to two-and-a-half times real memory. In these days, where 2G RAM is not unusual, and many times is not that uncommon, is this unreasonable? What is a reasonable size for swap these days?

It appears that there is no real hard and fast rule anymore. And setting it against a static value (like physical memory) is incredibly wasteful. It’s a much better idea to set it interactively. In other words, use the system without adjusting the Virtual Memory for a while, then take a look at your usage and set your virtual memory against that usage.

For instance:
If you’re in a Windows machine, let it run normally for a few days.
Run everything the way you normally use it.
– Multiple apps,
– Multiple instances,
everything you normally run.

Then open up the Task Manager and look at the Performance tab.
Take a look at the Peak value under “Commit Charge”.
Set your virtual memory, min and max, at about 10% above that value to leave yourself a little headroom. Normally this will be enough to deal with your maximum swap requests.
If, somehow, you begin bumping against virtual memory limits again AFTER that, bump it another 10%.
If you still have problems, keep bumping it in 10% increments, and start looking for apps that are memory leaking.

You have to be aware of what your system is doing and what its based on. For example, the best *current* laptop drive is still slower than a fairly low-end *current* desktop drive. A current laptop drive however is much faster than a high-end desktop drive from just a couple years ago however.

The point here is that if a laptop hard drive powers down, then swapping gets REALLY slow. On the other hand, you really don’t want to be swapping on a laptop or your battery life disappears… (unless you’re using a laptop / notebook as a desktop replacement)

Some background
Swap has two purposes: To swap out uncommonly-used pages so you can load more of the current working set, and to extend the memory space available to programs. 10 years ago, when we had mere seconds of swap, this made more sense. You could load windows, and swap half of it out, and boy did you ever need that RAM. If you were going to load a Word document that didn’t quite fit, swap could save your bum, and if it got too loaded, it’d still run out of memory. You could afford to wait because you only had a few seconds of swap, and short of a really pathological swapping pattern you could expect your operation to terminate in a couple of minutes.

The second use in modern times is basically pointless; just let the program fail. You almost certainly do not want to let the program spend the next hour swapping. The first use might still be valid, but you might as well only use a couple of seconds of swap; call it 100MB. Beyond that, nothing is really useful. And in Windows, excessive swapping also tends to basically lock up the machine which is something to be avoided.

Huge swap partitions, unless you have another use for them (some implementations of Linux suspend are the only ones I know of) are basically a holdover from the past, religiously clung to by people who have been ‘doing it this way’ for over a decade. Skip the swap unless you know why you shouldn’t.

Why do I talk about ’seconds of swap’ rather than gigabytes of swap ?
Processors have gotten faster, memory has gotten somewhat faster, hard drives have gotten monstrously larger, but hard drives have only gotten marginally faster since ten years ago.

So, instead of measuring swap in terms of absolute size, measure it in terms of time-to-fill, if your system were writing flat-out.Let’s say you have 1GB of memory and you use the old timey rule-of-thumb for 2.5GB of swap. Using an average hard drive transfer of 50GB/sec (which would be the read speed, not the write speed which is much slower), if you’re going to use that swap, it would take you nearly a minute to use that space, writing flat out.

Odds are you’re not actually going to use it that way, so whatever is using that swap is probably going to take a lot longer, orders of magnitude more. Do you really need a minute’s worth of swap? Is there anything that is so critical that you can’t afford to run out of memory, but you’re willing to wait hours upon hours while the swap is fully utilized?

Some more background
Most operating systems, desktop and server, will swap well ahead of not having free memory (for good reason). This isn’t a bad thing generally.

There are generally two strategies:
-The common-sense one
This where you swap when you run out of memory. This makes a lot of practical sense on systems with limited write cycles (flash based swap, though you really never ever should do that anyway), and systems that want to spin down drives to conserve power for battery conservation. Performance wise (this may surprise people who haven’t spent time thinking about it), this can often be bad. Avoiding swapping is generally only good on systems where resource utilization is carefully managed and you know it won’t swap ever (the unneeded IO operations can interfere with the productive activity of a constantly busy system). This is actually a vast minority of systems in the world (no matter how l33t one may think themselves, they most certainly don’t have a usage pattern that would be impacted by the extraneous IO operations of occasional write to swap.

-Pre-emptive swapping. When the IO subsystem is idle and the system can afford to copy memory to swap area, it does so (depending on criteria). Generally speaking it will select memory not accessed much and write it to disk, but leave the memory copy in place if the physical memory is not immediately needed. A fair amount of swap used in an apparently underutilized system is duplicated in physical memory and swap space. The benefit here is that if the process reads back that memory, it doesn’t incur any penalty in reading it back despite it being also in swap (the system may make certain decisions on what is the best swap candidate and write to disk different data). The benefit of writing this stuff to swap even when not needed is clear when an application comes along that allocs more memory than the system has free in physical space. In the first strategy, this means the malloc blocks while data is written to disk, and the new application starting or needing a lot of data is severely impacted. In the pre-emptive swap case, the system notices the condition, knows what memory it has a backup in swap of that hasn’t been used lately, and can free that memory and satisfy the malloc pretty much instantly.

To those who have, say, a desktop with 1GB of RAM or more, it becomes less likely that the system will have to flush memory from physical RAM, but there is a balance to be struck between memory used directly invoked by applications, what the application memory access pattern is, and what ram you can use to buffer filesystem access. If your total application memory allocation is 75%, it still may make sense performance wise to only keep 50% of your physical memory dedicated to the applications, (the other bit relegated to swap), and 50% of the memory to buffer disk I/O.

A couple of usefull links
This extensive and fantastically informative guide on tweaking virtual memory was a huge help and a fascinating read.

Topics: Microsoft, Technology | No Comments »

Comments

  • SAP Search

    Custom Search
    Add to Google
  • Recent Posts

  •  

    September 2006
    M T W T F S S
    « Aug   Oct »
     123
    45678910
    11121314151617
    18192021222324
    252627282930  
  • Archives