Effect of RAM on performance

From Schoolcoders wiki
Jump to: navigation, search

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:

  • You might not be able to start new programs.
  • Programs that are already running might fail with an error.


Virtual memory

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:

  • The system selects a block of physical memory (Block A) which is currently in use, and copies its contents to the swap space.
  • That block of memory is now "free", so it can be allocated it as Block B to the program which needs it.

This is all fine until a program tries to access Block A, which has been copied to the swap space. In that case:

  • The system selects another bock of physical memory (Block C), and copies it to the swap space.
  • It them copies Block A from the swap space into the physical memory where Block C was previously held. Block A can now be accessed.

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. It often makes the computer run very slowly, and is a sign that the computer needs more RAM.


Memory hierarchy

As a summary, here is the hierarchy of different types of memory, from the fastest (registers) to the slowest (virtual memory, on disk):

Hierarchy.png

See also