Saturday, November 8, 2008

Virtual Memory Optimization Guide

Virtual Memory
Back in the 'good old days' of command prompts and 1.2MB floppy disks, programs needed very little RAM to run because the main (and almost universal) operating system was Microsoft DOS and its memory footprint was small. That was truly fortunate because RAM at that time was horrendously expensive. Although it may seem ludicrous, 4MB of RAM was considered then to be an incredible amount of memory.
However when Windows became more and more popular, 4MB was just not enough. Due to its GUI (Graphical User Interface), it had a larger memory footprint than DOS. Thus, more RAM was needed.
Unfortunately, RAM prices did not decrease as fast as RAM requirement had increased. This meant that Windows users had to either fork out a fortune for more RAM or run only simple programs. Neither were attractive options. An alternative method was needed to alleviate this problem.
The solution they came up with was to use some space on the hard disk as extra RAM. Although the hard disk is much slower than RAM, it is also much cheaper and users always have a lot more hard disk space than RAM. So, Windows was designed to create this pseudo-RAM or in Microsoft's terms - Virtual Memory, to make up for the shortfall in RAM when running memory-intensive programs.

How Does It Work?
Virtual memory is created using a special file called a swapfile or paging file.
Whenever the operating system has enough memory, it doesn't usually use virtual memory. But if it runs out of memory, the operating system will page out the least recently used data in the memory to the swapfile in the hard disk. This frees up some memory for your applications. The operating system will continuously do this as more and more data is loaded into the RAM.
However, when any data stored in the swapfile is needed, it is swapped with the least recently used data in the memory. This allows the swapfile to behave like RAM although programs cannot run directly off it. You will also note that because the operating system cannot directly run programs off the swapfile, some programs may not run even with a large swapfile if you have too little RAM.
Swapfile Vs. Paging File
We have all been using the terms swapfile and paging file interchangeably. Even Microsoft invariably refers to the paging file as the swapfile and vice versa. However, the swapfile and paging file are two different entities. Although both are used to create virtual memory, there are subtle differences between the two.
The main difference lies in their names. Swapfiles operate by swapping entire processes from system memory into the swapfile. This immediately frees up memory for other applications to use.
In contrast, paging files function by moving "pages" of a program from system memory into the paging file. These pages are 4KB in size. The entire program does not get swapped wholesale into the paging file.
While swapping occurs when there is heavy demand on the system memory, paging can occur preemptively. This means that the operating system can page out parts of a program when it is minimized or left idle for some time. The memory used by the paged-out portions are not immediately released for use by other applications. Instead, they are kept on standby.
If the paged-out application is reactivated, it can instantly access the paged-out parts (which are still stored in system memory). But if another application requests for the memory space, then the system memory held by the paged-out data is released for its use. As you can see, this is really quite different from the way a swapfile works.
Swapfiles were used in old iterations of Microsoft Windows, prior to Windows 95. From Windows 95 onwards, all Windows versions use only paging files. Therefore, the correct term for the file used to create virtual memory in current operating systems is paging file, not swapfile.
Because both swapfiles and paging files do the same thing - create virtual memory, people will always refer to swapfiles and paging files interchangeably. Let's just keep in mind their innate differences.
Do We Still Need A Paging File?
Even today, when the average home user's computer comes with at least 256MB of RAM, the paging file is still very important. While the large amount of RAM in the average user's computer makes the risk of memory shortage much less of a worry with single applications now than it was back then; the paging file is essential when multi-tasking.
Note that over the years, the emphasis has changed to multi-tasking. No longer will people be solely stuck to using one application at a time. In fact, it is common to have 10 or more applications running simultaneously!
For example, I normally have the following applications running at the same time :-
+ Microsoft Outlook + Internet browsers like Maxthon and Firefox + An FTP client + Instant messengers like Trillian and MSN Messenger + A download manager like FlashGet + Macromedia Dreamweaver + P2P clients like ShareAza + An antivirus software + Adobe Acrobat Reader with a few PDF documents opened
That's a total of 10-12 applications running simultaneously!
Even with 256MB of RAM, it would be impossible to load everything into memory. A paging file is needed to store the least used data in the memory so that I can open up all those applications I need. And let's not forget the disk cache.
Operating systems like Windows 98 and Windows XP allocate a sizeable portion of the RAM to the disk cache. This speeds up accesses to hard disk data by caching the most frequently used as well as data that are most likely to be accessed next by the computer. This cuts down on the amount of available RAM. So, without a paging file, you won't be able to open many applications even if your computer has 256MB of RAM.
Finally, some programs require the use of a paging file to function properly. It may be to store sensitive data on something less volatile than the RAM or to ensure the computer will have sufficient memory to run it. But whatever the reasons, a paging file is needed in order for these programs to run.
Why Optimize The Paging File?
Unless your computer is truly loaded with RAM, it will almost always use the paging file. As such, its performance affects the performance of the whole computer.
Now, using a paging file may sound like a really cheap way to run memory intensive programs without the expense of buying more RAM. However, even the fastest hard disk is more than an order of magnitude slower than the slowest RAM.
Even the fastest hard disk is currently over 70X slower than the dual-channel PC2700 DDR memory common in many computers. Let's not even start comparing the hard disk with faster RAM solutions like PC3200 DDR memory or PC2-4200 DDR2 memory.
So, paging file is only a stopgap solution for the lack of sufficient RAM. As long as you use the paging file, there will always be performance degradation. The ideal solution for insufficient RAM is always more RAM, not more Virtual Memory. But since we can't afford all the RAM we want, a paging file is necessary for us to run today's memory guzzling programs.
As you can tell, more isn't better for the paging file because more paging file space will only give you the ability to run more memory intensive programs at once. It will not speed up your system. But what we can do is to optimize the paging file so that the performance degradation when using it is minimized.

So How Do We Optimize The Paging File?
There have been many theories on how to optimize the paging file. The most important ones are listed below :-
+ Making the paging file contiguous. + Moving the paging file to the outer tracks of the hard disk. + Creating a huge paging file. + Moving the paging file to a different partition in the same hard disk. + Moving the paging file to a different hard disk. + Creating multiple paging files + Moving the paging file to a RAID array + Moving the paging file to a RAM drive + Reducing reliance on virtual memory
We will examine those methods and see what will work and what won't.
Virtual Memory Then And Now
Windows 3.1
Back in the good old days of DOS 6.22 and Windows 3.1, everyone knew that creating a permanent swapfile was the key to optimal swapfile performance. This was because Windows 3.1 only creates permanent swapfiles that are contiguous.
A contiguous swapfile is a swapfile that consists of an uninterrupted block of hard disk space. When a swapfile is contiguous, the read-write heads of the hard disk can read and write data on the swapfile in a continuous fashion.
In Windows 3.1, if the swapfile was set up as a temporary swapfile which is created every time Windows 3.1 boots up, it will end up at the end of the hard disk and fragmented too. So, every time the swapfile is read from or written to, the hard disk heads have to seek all over the platters to conduct those operations.
Needless to say, this greatly erodes the performance of the swapfile. That's why it was important to make the swapfile permanent in Windows 3.1 - so that the swapfile will become contiguous.

Windows 95 And Above
From Windows 95 onwards, Microsoft encouraged the use of its new dynamic virtual memory system. Of course, there is nothing new about the virtual memory part but the keyword in this new technique is dynamic.
The new dynamic virtual memory system no longer relies on a fixed-size swapfile but a paging file that dynamically resizes itself according to need. When the computer runs out of memory, more memory is created by increasing the size of the paging file. Once the virtual memory is freed up, theoretically the paging file diminishes in size.
Microsoft claims that while its dynamic virtual memory system will create a fragmented paging file, it is still faster than Windows 3.1's static virtual memory system. As a bonus, no hard disk space will be tied up in a permanent paging file.
However, this dynamic virtual memory system does have a big disadvantage - it cannot be moved to the outer tracks of the hard disk platters.
Dynamic Paging Files And Data Locality
There are people who assert that when left alone, Windows XP will actually create virtual memory pages in close proximity to frequently-used data in the hard disk, like documents. In other words, they claim that Windows XP monitors disk usage, maintains a database of frequently-used files in the hard disk and uses that information to create the paging file based on spatial locality.
With virtual memory pages created close to frequently-used data, this apparently allows shorter seeks between frequently-used data and the paging file. That is the premise behind their theory of letting Windows XP handle the paging file dynamically. However, I don't think this is true at all.
First of all, while Windows XP does monitor disk usage and maintain a database of frequently-used files, only disk defragmenting utilities use that database. The built-in Disk Defragmenter, as well as third-party disk defragmenting utilities, use this database to rearrange the hard disk so that frequently-used data.
But as far as I'm aware, the paging file does not arrange the location of the pages according to this database. From my observations, Windows XP simply uses the nearest available clusters for the dynamic paging file.
In fact, Microsoft states that if you create multiple paging files, Windows XP will favour the partition that is least active. This completely refutes the theory of virtual memory pages being allocated according to spatial locality. Here is a quote from Microsoft's Knowledge Base.
By design, Windows uses the paging file on the less frequently accessed partition over the paging file on the more heavily accessed boot partition. An internal algorithm is used to determine which paging file to use for virtual memory management.
In any case, it doesn't make sense for Windows XP to create the paging file based on spatial locality to work files like your documents. Once opened, Windows keeps the working copy in the Temp folder, not your paging file.

No comments: