I was reading a newsletter the other day that was using he words Swap File, Paging File and Virtual Memory, interchangeably. Now this confused me and I realized that this might be a great topic for an article. I’ve been around computers since the age of 7, and I still find those things rather confusing.
I’ve previously written about RAM (Read It Here) so I’m going to assume you have a good grip on that topic. Now let’s dig into the world of Virtual Memory.
Some definitions first
The world of Computer Memory is a confusing one, not helped by the use of many different words and expressions for the same thing. To make things bit less confusing let’s narrow things down.
MEMORY in this article will always refer to RAM, the physical memory available to your computer.
DISC means Hard-Drive or the physical storage media on which all your data is stored.
SWAP FILE, SWAP DISK, PAGING FILE, VIRTUAL MEMORY. In Norway we have a saying: “A loved child, has many names”, which means: “Same thing, different name”. .
OS, means Operating System. The OS is the main program on your computer which controls everything else.
Virtual Memory Explained
As you know, every computer need a physical amount of RAM to be able to run. And one of the tasks performed by the OS is to manage and divide memory space for each running application (including its own needs). Of course the more Memory your computer can access at one given time, the better (and faster) it will perform. But what happens when it runs out of available memory ?
The first thing your OS does, is to reallocate memory. This means it evaluates each running application and decide its importance. Applications deemed “not that important at the moment” will temporarily get reduced resources and the application in dire straits will get more.
At one point this will no longer be possible. Then there are two choices. In earlier days you would get a message from the OS telling you to close unneeded applications to free up memory. These days the more common solution is the use of Virtual memory.
A hard-drive is not Memory
When the need to free up memory arise, and the OS have exhausted its resources – the use of disk space comes to the rescue. The problem is just that a hard-drive (or disk) is not designed to deliver data at the speed required to compute data. The nature of the disk makes it impossible to write and retrieve data fast enough. So how can the OS benefit from using the disk as temporary memory space ?
When necessary the OS can dump information from memory to the disk. That information is then temporarily unavailable. When the information is needed again, new data from the memory is dumped to the disk and the “old” data is written back into the available space. This is called Swapping (hence, Swap-File or Paging-file). On computers with a small amount of memory this happens more often than in systems with large amount of memory. The use of virtual memory can be detected by the familiar “grinding” sound your disk drive makes.
To sum up: Virtual Memory is actually nothing more than temporary storage used by the OS to free up available memory for an application requesting more resources. The storage is known as Swap-Files or Paging Files.
Can I tweak the virtual memory to increase performance?
The amount of memory that can be stored on your disk is pre-allocated. This means that the OS create a file of a set size, which the memory-controller can use at will. Much like a text document that you edit on a regular basis, adding and removing text.
The more disk space you allocate (file size allowed) the more memory can temporarily be freed. How large file (disk space) you need is determined by your usage and how much real memory you have.
How much disk space should I allocate ?
This is where the learned disagree. A common rule is 3 times the amount of RAM you have. In my opinion, computers with less than 1 GB RAM should allocate a swap file of at least 3 GB (If your computer needs to calculate large amount of data (like Photoshop) you might need to allocate even more space than that. On Computers with more RAM 3-4GB of RAM the Swap-File might not even be needed at all.
Which disk should have a Swap-File available ?
If you have more than one disc at your disposal, you may benefit from having a Swap-File on the other disc only. Leaving your Main Disk free to concentrate on the more important things. This will cut waiting time and thereby increase your computers overall performance.
Can the virtual memory be fragmented ?
It’s common knowledge that a heavily used disk gets fragmented. This may also apply to the swap file. The swap-file is treated (by the OS) as a disk drive and information in it, is stored much the same way as a real disk. A fragmented swap-file may be the cause of unexplainable slow computer performance or heavily usage of virtual memory.
So can it be de-fragmented ?
Yes it can. But not using the common programs that defragment the disk (like this one). This is because those programs doesn’t edit the files it relocates. They simply make sure that files are stored as a contiguous files.
In Windows Pre 7 you need a special program to defragment your virtual memory: PageDefrag which is a freeware tool from Windows Sysinternals. It is however recommended that you first run a normal disk defragmentation before trying to defrag your swap-file. You can read more about PageDefrag here. NOTE! The PageDefrag was developed for Windows 2000/NT and XP and might not work on Windows 7. I have tested it on my laptop with 32 bit win7, but My 64-bit does not like it.
In Windows 7 you can just as easy delete the Swap-File (Paging File) and create a new one upon reboot.
A more detailed instruction on how to actually tweak the virtual memory will be subject in a later article.
If you have comments on this topic or you feel I left something out, please leave your comments below.