Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

For simplification let's only talk about hard disks and RAM. In a nut shell, virtual memory is when "memory addresses used by a program.. [are mapped] into physical addresses in computer memory". By computer memory they mean any memory, including hard drives, right (not just RAM)?

Thrashing is defined as "when the hard drive is being overworked by moving information between the system memory and virtual memory". So this implies there's a transfer of data from the hard disk, back onto the hard disk but the section designated for virtual memory? If this is the case then why not just have all the contents of the hard disk always virtual memory?

EDIT: I see everyone is saying the purpose of virtual memory is so that RAM appears to be bigger, but isn't the point also having to do with the adress space inside processes, for example each process appears to have it's own address space? So virtual memory doesn't only have to do with more size...

share|improve this question

5 Answers 5

Basically virtualized memory uses real-memory as a cache for some of the virtual-memory space. If the virtual-memory grows too large to keep in real-memory, the system writes some of it to disk and frees that real-memory for other parts of the virtual-memory. The virtual-memory written to disk was probably never on the disk, so it has to be written there at that time.

Some older computers did use disk as memory, but having a real-memory as a cache is much MUCH faster.

When a running program accesses part of the virtual memory that is not in real-memory, but on the disk, the system gets a "page-fault" and reads that memory from the disk, but it's 1000+ times slower than using real memory.

Not all the disk is used as virtual memory (but it could be). Some is used for non-volatile storage (memory that stays after the power is turned off).

Did that answer your question?

share|improve this answer
    
"The virtual-memory written to disk was probably never on the disk, so it has to be written there at that time." that was my misunderstanding. But say you just downloaded a 1GB movie. So 1GB of data has been written to disk. If the user tries to watch the movie, does it get copied into virtual memory? If yes, does it get copied into the page file first (and since the page file is on the hard disk that would be like copying from one part of the hard disk to another) or does it get loaded straight in RAM? –  Celeritas Dec 11 '13 at 7:13
1  
Modern computers use memory as a cache for disk, so if you downloaded a 1G movie and watched it, your system might go to read part of it from disk, but then check and find it's already in memory. On the other hand maybe you rebooted before watching the movie and it must be read from the disk, but only a little at a time... If the movie viewer uses "mmap" to make a file appear in virtual memory then it would never have to write it to disk while reading it, but if it read 5 meg and modified it and then ran out of memory and swaps that out, then it has to write that to disk. –  9mjb Dec 11 '13 at 7:36
1  
The only time it has to write some virtual-memory to disk is when it runs out of real-memory and doesn't know that virtual-memory is already on the disk. Think of memory as a cache for disk and as holding part of the virtual-memory, and part of the virtual-memory is on disk if needed. The system tries to use it's resources as best it can. –  9mjb Dec 11 '13 at 7:42
    
Also you can use ram as a really fast disk (ram-disk). Obviously you don't swap virtual-memory onto that disk, and it loses it's content when the power goes off. A ram-disk can allow you to point software that wants a disk at memory for those reasons (speed and lose it when the power goes off). –  9mjb Dec 11 '13 at 7:46
    
"purpose of virtual memory is so that RAM appears to be bigger"... there are 2 purposes (maybe more?) ... 1 is to make each process have it's own memory space, and probably all starting at address zero, and ...2 allow using disk as extra memory. Check Wikipedia: en.wikipedia.org/wiki/Virtual_memory –  9mjb Dec 11 '13 at 7:58

How is virtual memory used?

When the operating system starts a process it allocates physical memory for the process and sets up a virtual memory (mapping). Each process has its own virtual memory mapping. The virtual memory addresses can be the same for each process (Windows liked to use 0x00400000 as process base address) but it can also be individual for each process. Where this virtual memory lies in the physical memory is totally dependant on the current situation and differs every time you run the process.

When a process accesses an address 0x12345678 then a lookup in the virtual memory mapping table has to be done to see which physical memory address has to be read. The process does not see that mapping, it is done transparently.

An example: Let's assume you have a process A. The process might start at virtual address 0x00400000 and have the length 10 MB. A second process B might also start at virtual address 0x00400000 and have the length 6 MB. Both processes start at the same virtual address, but the physical memory of both processes is different. Process A might use 10 MB at physical memory 0x00010000 and process B might use 6 MB at physical memory 0x00200000.

What's the use of virtual memory?

  1. Virtual memory allows separation of processes. Process A can only access physical memory that is mapped into its virtual memory. Process A can not mess with process B's memory. This is a security issue (process A cannot read secrets from process B's memory) and a stability issue (process A will not mess with other processes memory when using a bad pointer, but crash with no effect on other processes).
  2. If you have a program that has 2 MB code, 1 MB constant data and 5 MB dynamic data and if you start that program twice, then process A and B will share the same physical memory for the code and the constant data segment. So you'll not need 2*(2+1+5) MB physical memory but 2+1+2*5 MB.
  3. Virtual memory does not necessarily need to be backed up by RAM. Virtual memory can be RAM, but it can also be disc space.
  4. Operating systems can give a process more memory than is actually backed up by physical memory (Google search: linux memory overcommit).

Thrashing does not occur if all your virtual memory is only backed up by RAM. It only occurs when your virtual memory is backed up by disc space. To understand that let's first take a look at how backing up virtual memory on disc works.

When a process accesses a virtual memory address, then the operating system must have the corresponding physical memory backed up in RAM. If this is already the case, then the process can just access the memory. If the physical memory is not in RAM but stored on a disc, then the operating system first needs to load the memory from disc to RAM. The process has to wait until the memory is loaded from disc and then the process will be resumed and can access the memory. If there is no free RAM where the operating system can load the memory from disc into, then the operating systems needs to free up some RAM. This can be done by moving (writing) any RAM memory to disc.

The problem here is how to decide which RAM memory to move to disc. In a worst case scenario you might have two processes running, process A accessing memory page 1 in a loop and process B accessing memory page 2 in a loop, and you have page 1 in RAM and page 2 on disc. When process B access page 2, page 1 might get moved to disc, page 2 moved to RAM and process B can access page 2. Then process A is scheduled and page 2 is moved to disc, page 1 is moved to RAM and process A can access page 1. If you keep on swapping the two pages the processes will perform very bad and you will see 100% disc activity. In this limited scenario it is quite obvious that other paging strategies should perform better. Quite sophisticated code exists that decides which memory to move from RAM to disc. One strategie for example would keep recently accessed pages and swap out the least recently used pages. Of course you a) cannot have an in all cases optimal strategy without looking into the future and b) the problem gets worth the more processes you have or the less RAM you have.

You ask why the operating systems does not just use the complete hard disc for virtual memory backup. In fact, in Windows for example you can create a file mapping which means that you can give Windows a file name and you get back a virtual memory address. If you read from/write to that virtual memory than Windows will automagically read from/write to the file on disc. This is a nice feature but it does not help with thrashing. You need to remember that the cause of thrashing is that you have not enough RAM for all processes. Adding more disc space does not help in this case.

share|improve this answer

Virtual memory is a memory space that is overlaid on top of the Physical Memory (RAM) as well as the Swap file/partition.

The Swap file/partition is a special file/section on the hard drive that the computer can use as RAM. When the swap file/partition is defined, there is nothing else that can use that space. So theoretically you could have 100 gigs dedicated to a swap file, but if you only had a 200 gig hard drive, that's half your space gone.

What virtual memory does, is allow a computer to use more memory than it actually has to run programs. To do this, it stores data that is no longer needed to be in the foreground, but is still required in the swap file/partition.

This allows a program to store large amounts of data that is ready to be accessed (addressed) but still allow other programs to do the same thing.

The reason why hard drives get thrashed when you don't have enough RAM is that the computer has to keep getting data for Program A from the Swap file/partition and putting it into RAM, but then when Program B needs access to its data, the computer then has to put the Program A's data in the RAM back onto the hard drive to make space for Program B's data.

RAM is also a lot faster than hard drives, which is why it's normally the first thing to upgrade when the computer starts thrashing it's swap file/partition

share|improve this answer
    
Before a program be loaded into RAM, must it past through the swap file first? –  Celeritas Dec 11 '13 at 7:01
    
So basically the swap file is what is used to implement the technique of virtual memory? –  Celeritas Dec 11 '13 at 7:02
1  
No, a program will go directly to RAM when it's run. However, if it then has more data than Physical RAM, then the extra data that doesn't fit will go into the Swap File (basically) –  Lawrence Dec 11 '13 at 7:11
    
and when you say "a program will go directly to RAM" that ram is part of the virtual memory, right? –  Celeritas Dec 11 '13 at 7:14
1  
Besides the RAM and the swap file there are also memory-mapped files. --- The statement above about swapping of executables from RAM is wrong. The read-only parts of executable file (the program code and constant data) do not need to be moved to the swap. They are just removed from the RAM because their copy is still available in the memory-mapped executable file. --- Also started program usually do not go immediately into the RAM. Parts of the memory-mapped executable file are loaded as they are needed. –  pabouk Dec 11 '13 at 10:41

Thrashing is defined as "when the hard drive is being overworked by moving information between the system memory and virtual memory"

Simplifying a bit, virtual memory is backed by disk, i.e. the pagefile in Windows or the swap partition in Linux.

So this implies there's a transfer of data from the hard disk, back onto the hard disk but the section designated for virtual memory?

Virtual memory, or the pagefile, holds the RAM of processes that have been "swapped out." Modern systems try to swap out processes to disk that haven't been used in a while, such as minimized programs you haven't touched in a while, or services that have been inactive for a long time.

This allows more real RAM for programs you are actually using. Also it allows you to get away with running more programs than your RAM will support. However, during "thrashing", basically you have multiple processes that won't fit in RAM, but are trying to be active and do work.

This can happen if there are two or more processes running simultaneously that are using a lot of memory in a system where all the memory is allocated. So the system is constantly moving data from disk, to RAM, and back to disk - i.e. process A needs something that was in RAM but was shunted to the page file, so the system retrieves it, but if memory is really low, it may have to swap another process's memory to disk. Then when process B gets its turn on the CPU, the system may need to go back to disk again and swap in something for process B.

If this is the case then why not just have all the contents of the hard disk always virtual memory?

Doesn't make sense. You're basically saying why not put the whole disk in the page file or swap partition. The page file or swap partition is just an area of disk meant to be used by the virtual memory system of the CPU/OS, but it isn't magically faster than the rest of the disk for any reason.

EDIT: I see everyone is saying the purpose of virtual memory is so that RAM appears to be bigger, but isn't the point also having to do with the adress space inside processes, for example each process appears to have it's own address space?

Each userspace process has its own address space. It begins at address 0 and goes up to the maximum that it was allocated by the OS. The CPU's MMU maps userspace memory addresses to actual memory addresses transparently to the userspace process.

You seem to be thinking "virtual memory" in this context is something different from your physical RAM, and it's not. It's merely another view of physical RAM created for a userspace process. So, if you have a 3TB hard disk, you can't even do this unless you have 3TB of real RAM, plus what you need to run your programs. Plus every time you update the RAM you'd have to update the hard disk, so unless you really need to read 3TB of data very fast, it's pointless.

share|improve this answer
    
"Doesn't make sense. You're basically saying why not put the whole disk in the page file or swap partition. The page file or swap partition is just an area of disk meant to be used by the virtual memory system of the CPU/OS, but it isn't magically faster than the rest of the disk for any reason." I think my misunderstanding was that if something wasn't in the swap space, it would need to be copied to the swap space (on disk) before going to RAM. In other words I was inserting a step between reading data from disk and putting in RAM. –  Celeritas Dec 11 '13 at 20:03

TO BE SIMPLE

Computer Memory is all the non persistent Storage. e.g RAM L1 L2 Cache etc.

Disk is persistent. No Memory there just Storage.

Virtual Memory is A combination of real Computer Memory and a spezial File on the Disk wich acts as Memory extension for the case that the C. Memory isn't enough also known as swap on linux and pagefile on Windows.

So no there is no copy from disk to disk. just ram to disk and back.

If you wan't to have more info on this i think googling "memory", "swap" or "pagefile" will help you.

share|improve this answer
    
but my point is on that special file (e.g. swap or pagefile or whatever you want to call it) the data has to be copied from the persistent storage on disk to the swap file each time, right? in other words, what happens to the swap file when the computer shuts down, it gets deleted right? –  Celeritas Dec 11 '13 at 7:04
1  
it does not get deleted –  Keltari Dec 11 '13 at 7:06
    
@Celeritas: Swap is normally never deleted. Its free parts are just being overwritten. --- When you shut down an operating system the virtual memory content (and its physical storage devices -- RAM and swap) is simply discarded. Nothing is deleted. It is just declared free to store new content. --- Normally unmodified data are not being copied from files to swap though this could happen with some poorly written applications as an unwanted effect. –  pabouk Dec 11 '13 at 10:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.