Tuesday, March 16, 2010

Virtual Memory

Hi again. In this post Im gonna discuss about virtual memory.Yes... youll find loads of material on virtual memory on the web.For all those preparing for interviews, its a sure shot question. Its kinda interviewers favorite question... So lets hit it....

Virtual Memory:
When we open some applications on our computer like an internet browser,mp3 player,text editors etc all at the same time, the pages of these applications are brought into RAM by computer. Now lets say, there is only 64MB of RAM in your computer and lets forget the concept of virtual memory. If there was no thing called virtual memory then this would mean that when your applications occupied 64MB in your RAM, then your computer would stop obeying your orders of opening new applications. Wont that be unfair to you, u spent lotsa money on that box and it defies you....

Here comes the role of virtual memory.....
This concept is based on the fact that when you run an application, not all the pages of the application are needed at once and also that about 10-20% of the pages are required to keep the application running...
Virtual memory lets you to bring the pages required at a particular instant into RAM and swap out those which are not being used.
These swapped out pages are kept on the hard disk. In linux, we have the swap filesystem whereas in windows we have pagefile.sys for this purpose.
The user is unaware of this whole mechanism and gets an illusion that there is indefinite amount of RAM at his disposal for him to open several applicatoins.

Let me also discuss now how the various memories are organized.

CACHE-------RAM------VIRTUAL MEMORY | HARD DISK

When a page is needed, the CPU first checks the page in cache memory. If the page is not found in cache, then cpu looks up for it in RAM. If found in RAM, the CPU keeps a copy of the page in cache so that, it is available for next demand from the cache itself.
If the page is neither found in RAM nor in the Cache, then the page is loaded from the virtual memory, which resides on the hard disk, into the RAM

No comments:

Post a Comment