Before you start
Objectives: Learn what is page (swap) file, what is swapping, and what is disk crashing situation.
Prerequisites: no prerequisites.
Key terms: ram, file, physical, virtual, hard, application, run, page, swap, process, performance
What is Virtual Memory
Virtual memory is a component of most operating systems, such as MAC OS, Windows and Linux. Virtual memory has a very important role in the operating system. It allows us to run more applications on the system than we have enough physical memory to support. Virtual memory is simulated memory that is written to a file on the hard drive. That file is often called page file or swap file. It’s used by operating systems to simulate physical RAM by using hard disk space. To understand how virtual memory works we have to go back in time, before virtual memory even exited. In the days of Windows version 1 or 2, we actually couldn’t run many applications if we didn’t have enough physical RAM installed. As we know, the system itself is using a portion of RAM. If we run more applications, each application will also get its own portion of RAM. If we run too many applications, at one point we will run out of RAM. At that point we won’t be able to open any additional application. Back in those days we had to live with that. If we don’t have enough memory, we can’t run applications.
Everything changed with the release of Windows 3.0 and the 8386 processor. With these two together we could use virtual memory. With virtual memory we still use our physically installed RAM, but we can also map RAM addresses to the hard drive. To do that, a portion of the hard drive is reserved by the system. That portion can be either a file or a separate partition. In the case of Windows it is a file calledpagefile.sys. Under Linux a separate partition is used for memory. When the system needs more memory it maps some of it’s memory addresses out to the hard disk drive. That means that we can run more applications than we have RAM installed. As far as the CPU is concerned there is enough memory to accommodate all applications. That extra memory doesn’t actually exist in RAM. It’s the storage space on the hard drive.
In Windows we can manage virtual memory through the Advanced tab of the System applet in Control Panel. Windows manages the virtual memory settings automatically, which should be sufficient in most cases. However, we can also manually configure the maximum size of the virtual memory swap file and the disk partition on which the swap file is stored. The recommended size for the swap file is 1.5 times the amount of physical RAM. For a slight increase in performance, we can move the swap file to a different physical disk than is being used by the operating system. However, if the paging file is on the system drive, Windows creates a memory dump file if the system crashes. This file can be used to help identify what caused the system crash.
What is Swapping
With virtual memory we also use a process called swapping. The process of moving data from RAM to disk (and back) is known as swapping or paging. The Virtual Memory Manager (VMM) is in charge of swapping data between physical memory and the hard disk. As we know, hard drive is a lot slower than RAM. Therefore if we’re using some application actively, we don’t want memory of that application to be stored on the hard drive. Let’s say that we opened two applications and that we have enough RAM only to hold only one application in it. Let’s say that one application is MS Word and another is MS Excel. Let’s say that currently we are writing some article in MS Word, and MS Excel is running in the background. Since we are actively using MS Word, we want all of MS Word required memory to be in the physical RAM, which will improve performance. In this case, because we don’t have room for the operating system and all opened applications at the same time, the system will swap our applications. In other words, the system will take the application that is currently being used, which is MS Word in our case, and will load all of it into RAM. MS Excel will be swapped out to virtual memory, since it’s not currently being used. It’s still running, but because it’s not being currently used, we can swap its memory out to the hard disk drive so that MS Word can run much faster. When we need to use MS Excel again and we switch to it, the system will take MS Word memory requirements and put it back onto the hard disk drive, and will take MS Excel memory requirements out of the paging file and load it back into physical RAM. That makes sure that the currently active application is running in physical RAM, which allows us to have reasonably good performance.
When there is no more space in physical RAM, the VMM will actually take the application that hasn’t been used for the longest period of time and place it in the page file on the hard drive. If the system needs to access an application that is located in virtual memory, VMM will first take the program in the physical RAM that hasn’t been used for the longest period of time and put it to the page file on the hard drive. Then it will take the the application currently being accessed back to the physical RAM. The process of taking an application from the physical RAM and putting it in the page file is called paging out. The process of moving the application from the page file back into physical RAM is called paging in. If we didn’t have virtual memory we would have to have considerably more RAM in our systems to run all the applications that we normally do. To check how much physical and virtual memory is being used we can open Task Manager and then go to the Performance tab. On the Performance tab we can see statistics for our physical and virtual memory.
Disk Crashing Situation
Virtual memory feature is great, but it is possible overuse it. When we do that the system slows down to a crawl rate. This can happen if we run too many applications, or if applications that we run require large amounts of memory. The problem is not necessarily with the page file itself since we can make it huge. We usually have a big hard drive, so we can use a lot of space on that hard drive for virtual memory. The problem is the fact that the swapping has to occur very frequently. If we run too many applications and the applications use too much memory, as opposed to the physical memory installed, then the swapping happens too frequently. In that case the system basically doesn’t do anything else but the swapping. In this situation we say that the system is undergoing disk thrashing. We know that this is happening because the hard disk light stays on all the time. It stays on because the hard disk is constantly moving information in and out of virtual memory. We can also see high disk statistics in Performance monitor. In the old days it was very easy to have disk thrashing situation because of the small amounts of RAM installed. Today disk thrashing happens less often, but it still can especially if we’re using applications that needs huge amounts of memory. If we’re having a problem with disk thrashing, we can try and close some applications and processes. The other thing we can do is to install more physical memory in the system.
Virtual memory is simulated memory that is written to a page file on the hard drive. In the case of Windows it is a file called pagefile.sys. The process of moving data from RAM to disk (and back) is known as swapping or paging. When there is no more space in physical RAM, the VMM will take the least used application and place it in the page file on the hard drive. The process of taking an application from the physical RAM and putting it in the page file is called paging out. The process of moving the application from the page file back into physical RAM is called paging in. Disk thrashing occurs when the amount of physical memory is too low. In that case the data must constantly be moved from physical RAM, to disk, and back again.