Every program requires a certain amount of RAM when it runs. Often a program will request a certain amount of memory when it first starts, and it may request more memory while it is running, depending on what it is trying to do.
If there are too many programs running on a computer, it might run out of RAM. This can cause problems:
To avoid these problems, computer systems often use virtual memory. Suppose your system has 8GB of actual (physical) memory. You can also request that the system uses 8GB of virtual memory. So your system would behave as if it had 16GB.
What actually happens is that the system will allocate 8GB of space on the hard drive, called swap space. Now suppose the physical RAM is completely full, and a program requests more memory. Here is what happens:
This is all fine until a program tries to access Block A, which has been copied to the swap space. In that case:
This is OK if you occasionally use slightly more memory than the system has available - it means the computer can keep running without any program errors. But if you use a lot of virtual memory, you can end up in a situation where the computer is continuously reading and writing memory to the swap file, and all the programs run at a snail's pace. This is called disk thrashing.
As a summary, here is the hierarchy of different types of memory, from the fastest (registers) to the slowest (virtual memory, on disk):
Copyright (c) Axlesoft Ltd 2021