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.
"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 – 2013-12-11T07:13:12.210
1Modern 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 – 2013-12-11T07:36:50.450
1The 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 – 2013-12-11T07:42:07.620
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 – 2013-12-11T07:46:37.610
"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: http://en.wikipedia.org/wiki/Virtual_memory
– 9mjb – 2013-12-11T07:58:37.037