VIRTUAL MEMORY
If you have ever queried about how the computer processes a data or execute the instructions then you must have known that it is the Central Processing Unit (CPU) of a computer that processes a data.  You must also have come across the fact that it is the Main memory from where the data is fed into CPU and after being processed, the results are stored back into it. From the Main memory the result or data is again either given to the output devices or stored somewhere in the specified memory locations of a Computer system. It means all the data, programs or an instruction that has to be processed has to be in the Main memory in order for the CPU to process the data or run the program. Main memory is also known as system memory. Examples of main memories are RAM (also known as the Read/Write memory) and the Cache Memory. 
Sometimes users want to use the computer to process so many programs at once. But the amount of RAM (or Main memory) is not enough to run all of the programs that the users expect to run at once. For example, if you load an operating system, an e-mail program, a Web browser, Maya software and a movie program into RAM simultaneously, then 512 MB of RAM is not enough to hold it all. In such a situation a computer is not able to run all the programs because RAM cannot accommodate all such programs and hence CPU cannot access it for processing. In such case, you have to close some applications and load a new one whichever you want to run.
To overcome this problem a computer uses a Virtual memory. Virtual Memory is some portion of Hard Disk (HDD) space that is used as the memory. It is used to store application data and instruction that is currently not needed to be processed by the system. So Virtual Memory can be said as a memory that uses a portion of HDD space as the memory to store the application data and instruction that the system deem not to process for now. In other sense, virtual memory is a technique of allowing instructions to be executed by a program that may not be able to fit entirely in main memory.
Working of Virtual Memory
During the program loading process, a computer can look for the application data and instruction that have not been used recently in a main memory and copy them onto the hard disk. This frees up space in main memory to load the new application. Again the system will copy the application data and its instruction from the HDD into the main memory.
Now the system can use its resources such as CPU to process and execute application data of a main memory. Once the main memory (system memory) gets filled up, the system moves some of the data and instruction that is not needed for processing into the Virtual Memory until those data and instruction is needed for processing again. In this way, the system manages to call the next application data and instruction and copy it into the main memory so that the system processes the rest of the program. When the data and instruction that is in the Virtual Memory needs to be processed again then the system first checks the main memory for its space. If there is space, it simply loads data and instructions in the main memory. If there are not any spaces left in the main memory, the system moves any data and instructions that don’t need to be processed to the Virtual Memory. And then swap the data and instruction that need to be processed by the system from the Virtual Memory into the main memory. This job of data transfer is done simultaneously so that the user feels that all the loaded applications are running at once. It makes your computer feel like it has unlimited RAM space even though it has limited memory space.
Methods for Data Transfer
Although you must have got idea how the virtual memory works, a program cannot actually access and manipulate data within the virtual memory. So it is the job of an Operating System to manage the transfer of data. Refer figure 1 to get the idea of virtual memory management.

There are two methods to manage the transferring of the data from virtual memory to main memory.
They are (1) Paging (2) Segmentation.
1. Paging
In this method the data being swapped is a fixed size. The area of the hard disk that stores the RAM image is called a page file. Paging uses a single address space. When the program needs data that is in virtual memory, the operating system copies a certain number of pages, blocks of data, from hard disk to main memory. When the required data is not in main memory, the operating system finds it into the hard disk and copies the correct page into main memory. Whenever a page is needed and if it is not currently in memory, a page fault occurs. When the address of the page being requested is invalid then ‘invalid page fault’ occurs. In this case, the application is usually aborted. An invalid page fault is one of the reasons computers crash.
2. Segmentation
This method uses variable sized memory blocks. This method is useful when the size of objects in a program change dynamically. In this method, if a single address space is used, as in the paging, then after the memory has been allocated its size cannot be changed. This will result in wastage of memory or memory may not be enough due to unnecessary usage. To overcome this problem the computer system manages to set up many independent address spaces. Each of these address spaces is called a segment. However, unlike paging, the programmer actively maintains the segments.
Size of Virtual Memory
Since the hard-disk space is much cheaper than RAM chips, virtual memory provides a nice economic benefit. But you should note that the read/write speed of a HDD is much slower than RAM, and the technology of a hard drive is not proper for accessing small pieces of data at a time.
You should set the size of your virtual memory depending upon your need and usage. Neither it is good to have too low of Virtual Memory size nor it is better to have too large Virtual Memory size (meaning the size that is too much compared to system memory). If you set the Virtual Memory too low, then the OS will keep issuing an error message stating that not enough memory or Virtual memory too low. If you set the Virtual Memory size too large to support the intensive application then it will create the performance lagging and will unnecessarily take the free space of HDD. The ideal size for the Virtual Memory is the default size of Virtual Memory, and it should not exceed the value of the triple size of system memory.
You can determine how much virtual memory you need depending upon the amount of RAM of your system. By default, the OS will set the appropriate size for Virtual Memory.
If your system has to rely too heavily on virtual memory then there will be a significant performance drop. So it is better to have enough RAM size to handle everything you want to run on simultaneously. When you have enough RAM for your needs, virtual memory works beautifully. If you don’t have enough RAM then the operating system has to constantly swap information back and forth between RAM and the hard disk. This is called thrashing, and it can make your computer feel incredibly slow.
From the above discussion it can be said that a virtual memory is a LOGICAL memory and is controlled by the Operating System. The advantage virtual memory gives to a programmer is that he needs to be aware of only the LOGICAL memory space. The LOGICAL memory space is a combination of physical memory and the hard disk space acting as memory.
Reference:
1. http://www.cs.umd.edu/~meesh/cmsc411/website/projects/virtual/virtual.html
2. http://computer.howstuffworks.com/question684.htm
3. http://www.cs.gmu.edu/cne/modules/vm/green/defn.html
4. http://faq.programmerworld.net/ms_windows/virtual-memory.html
Sometimes users want to use the computer to process so many programs at once. But the amount of RAM (or Main memory) is not enough to run all of the programs that the users expect to run at once. For example, if you load an operating system, an e-mail program, a Web browser, Maya software and a movie program into RAM simultaneously, then 512 MB of RAM is not enough to hold it all. In such a situation a computer is not able to run all the programs because RAM cannot accommodate all such programs and hence CPU cannot access it for processing. In such case, you have to close some applications and load a new one whichever you want to run.
To overcome this problem a computer uses a Virtual memory. Virtual Memory is some portion of Hard Disk (HDD) space that is used as the memory. It is used to store application data and instruction that is currently not needed to be processed by the system. So Virtual Memory can be said as a memory that uses a portion of HDD space as the memory to store the application data and instruction that the system deem not to process for now. In other sense, virtual memory is a technique of allowing instructions to be executed by a program that may not be able to fit entirely in main memory.
Working of Virtual Memory
During the program loading process, a computer can look for the application data and instruction that have not been used recently in a main memory and copy them onto the hard disk. This frees up space in main memory to load the new application. Again the system will copy the application data and its instruction from the HDD into the main memory.
Now the system can use its resources such as CPU to process and execute application data of a main memory. Once the main memory (system memory) gets filled up, the system moves some of the data and instruction that is not needed for processing into the Virtual Memory until those data and instruction is needed for processing again. In this way, the system manages to call the next application data and instruction and copy it into the main memory so that the system processes the rest of the program. When the data and instruction that is in the Virtual Memory needs to be processed again then the system first checks the main memory for its space. If there is space, it simply loads data and instructions in the main memory. If there are not any spaces left in the main memory, the system moves any data and instructions that don’t need to be processed to the Virtual Memory. And then swap the data and instruction that need to be processed by the system from the Virtual Memory into the main memory. This job of data transfer is done simultaneously so that the user feels that all the loaded applications are running at once. It makes your computer feel like it has unlimited RAM space even though it has limited memory space.
Methods for Data Transfer
Although you must have got idea how the virtual memory works, a program cannot actually access and manipulate data within the virtual memory. So it is the job of an Operating System to manage the transfer of data. Refer figure 1 to get the idea of virtual memory management.
There are two methods to manage the transferring of the data from virtual memory to main memory.
They are (1) Paging (2) Segmentation.
1. Paging
In this method the data being swapped is a fixed size. The area of the hard disk that stores the RAM image is called a page file. Paging uses a single address space. When the program needs data that is in virtual memory, the operating system copies a certain number of pages, blocks of data, from hard disk to main memory. When the required data is not in main memory, the operating system finds it into the hard disk and copies the correct page into main memory. Whenever a page is needed and if it is not currently in memory, a page fault occurs. When the address of the page being requested is invalid then ‘invalid page fault’ occurs. In this case, the application is usually aborted. An invalid page fault is one of the reasons computers crash.
2. Segmentation
This method uses variable sized memory blocks. This method is useful when the size of objects in a program change dynamically. In this method, if a single address space is used, as in the paging, then after the memory has been allocated its size cannot be changed. This will result in wastage of memory or memory may not be enough due to unnecessary usage. To overcome this problem the computer system manages to set up many independent address spaces. Each of these address spaces is called a segment. However, unlike paging, the programmer actively maintains the segments.
Size of Virtual Memory
Since the hard-disk space is much cheaper than RAM chips, virtual memory provides a nice economic benefit. But you should note that the read/write speed of a HDD is much slower than RAM, and the technology of a hard drive is not proper for accessing small pieces of data at a time.
You should set the size of your virtual memory depending upon your need and usage. Neither it is good to have too low of Virtual Memory size nor it is better to have too large Virtual Memory size (meaning the size that is too much compared to system memory). If you set the Virtual Memory too low, then the OS will keep issuing an error message stating that not enough memory or Virtual memory too low. If you set the Virtual Memory size too large to support the intensive application then it will create the performance lagging and will unnecessarily take the free space of HDD. The ideal size for the Virtual Memory is the default size of Virtual Memory, and it should not exceed the value of the triple size of system memory.
You can determine how much virtual memory you need depending upon the amount of RAM of your system. By default, the OS will set the appropriate size for Virtual Memory.
If your system has to rely too heavily on virtual memory then there will be a significant performance drop. So it is better to have enough RAM size to handle everything you want to run on simultaneously. When you have enough RAM for your needs, virtual memory works beautifully. If you don’t have enough RAM then the operating system has to constantly swap information back and forth between RAM and the hard disk. This is called thrashing, and it can make your computer feel incredibly slow.
From the above discussion it can be said that a virtual memory is a LOGICAL memory and is controlled by the Operating System. The advantage virtual memory gives to a programmer is that he needs to be aware of only the LOGICAL memory space. The LOGICAL memory space is a combination of physical memory and the hard disk space acting as memory.
Reference:
1. http://www.cs.umd.edu/~meesh/cmsc411/website/projects/virtual/virtual.html
2. http://computer.howstuffworks.com/question684.htm
3. http://www.cs.gmu.edu/cne/modules/vm/green/defn.html
4. http://faq.programmerworld.net/ms_windows/virtual-memory.html
Comments
Post a Comment