Primary Storage: RAM, ROM and Virtual Memory
Why the computer needs memory at all, the real difference between RAM and ROM, and what virtual memory is actually doing when your machine slows to a crawl.
Why primary memory exists
The CPU can only fetch instructions and data from primary memory. It cannot read directly from a hard disk or an SSD.
So when you open a program, the operating system copies it from secondary storage into RAM, and the CPU works from there.
Why not just run it from the hard drive? Because secondary storage is thousands of times slower. If the CPU had to wait for a disk on every instruction, a 3 GHz processor would perform like a machine from the 1970s.
Primary storage is memory that the CPU can access directly. It holds the data and instructions that are currently in use.
The memory hierarchy
Speed and cost pull in opposite directions, which produces a hierarchy:
| Level | Speed | Capacity | Cost per byte | Volatile |
|---|---|---|---|---|
| Registers | Fastest | A few bytes | Highest | Yes |
| Cache | Very fast | KB to MB | Very high | Yes |
| RAM | Fast | GB | Moderate | Yes |
| Secondary storage | Slow | GB to TB | Low | No |
The pattern is consistent: the closer to the CPU, the faster, the smaller and the more expensive.
- Registers: inside the CPU, a few hundred bytes, fastest of all.
- Cache: still on the chip, a few megabytes, and very fast.
- Main memory: gigabytes, fast, and it empties when the power goes.
- Secondary storage: terabytes, slow, and it keeps your files forever.
- The whole point: small and fast at the top, huge and slow at the bottom.
RAM
RAM, random access memory, is the computer's main memory.
- It holds the operating system, the applications currently running and the data those applications are using.
- It is volatile: its contents are lost the moment power is removed.
- It can be read from and written to.
- It is much faster than secondary storage but slower than cache.
Volatile does not mean unstable or unreliable. It means the contents disappear when the power goes. This is why unsaved work is lost in a power cut.
What happens when RAM fills up
RAM is finite. Open enough programs and it runs out. The computer does not simply stop, it starts using virtual memory, and that is when things get slow.
ROM
ROM, read only memory, holds instructions that must be available the instant the machine is switched on.
- It is non volatile: contents are kept when the power is off.
- It is read only in normal use, so the contents cannot be accidentally overwritten.
- It is small, typically a few megabytes at most.
- It contains the bootstrap or BIOS: the start up instructions.
The boot process
- The computer is switched on.
- The CPU fetches the boot program from ROM, because RAM is empty at this point.
- The boot program checks the hardware is working, a power on self test.
- It then locates the operating system on secondary storage and copies it into RAM.
- Control is handed to the operating system, which is now running from RAM.
"Explain why a computer needs both RAM and ROM." The answer hinges on volatility. RAM is volatile, so at switch on it is empty and cannot hold the start up instructions. ROM is non volatile, so it still holds the boot program. ROM alone is not enough because it cannot be written to, and running programs constantly need to write data.
| RAM | ROM | |
|---|---|---|
| Full name | Random access memory | Read only memory |
| Volatile | Yes | No |
| Read and write | Both | Read only in normal use |
| Typical size | 8 GB to 32 GB | A few MB |
| Contents | Operating system, open programs, data in use | Boot program, BIOS |
| Speed | Fast | Fast |
Virtual memory
Virtual memory is a section of secondary storage used as if it were RAM, when RAM is full.
How it works
- RAM becomes full but another program needs to be loaded.
- The operating system identifies data in RAM that has not been used recently.
- That data is moved out to a dedicated area of secondary storage, called the swap file or page file.
- The freed RAM is used for the new program.
- If the moved out data is needed again, it is swapped back into RAM, which may require moving something else out.
Why it makes the computer slow
Secondary storage is far slower than RAM. Every swap means writing data out and reading data in. If the machine is very short on RAM this can happen constantly, a condition called disk thrashing, where the computer spends more time moving data than doing useful work.
Virtual memory lets a computer run more programs than its RAM can physically hold, at the cost of a large drop in speed, because secondary storage is much slower than RAM.
The fix
Adding more RAM. With enough RAM, virtual memory is rarely needed, so the swapping stops.
- RAM holds the programs that are running. It is fast, and it is small.
- Open one program too many and there is no room left.
- A page that has not been used lately is written out to the disk.
- That frees real memory, so the new program can start.
- Needed again, the page is fetched back, which is why it goes slow.
Key terms you must be able to define
- Primary storage
- Memory the CPU can access directly, holding data and instructions currently in use.
- RAM
- Random access memory. Volatile main memory holding the operating system, running programs and data in use.
- ROM
- Read only memory. Non volatile memory holding the boot program the computer needs at switch on.
- Volatile
- Loses its contents when power is removed.
- Non volatile
- Keeps its contents when power is removed.
- Virtual memory
- An area of secondary storage used as extra RAM when RAM is full.
- Swap file
- The area of secondary storage where data moved out of RAM is held.
- Bootstrap
- The small start up program held in ROM that loads the operating system into RAM.
- Disk thrashing
- Excessive swapping between RAM and virtual memory, which makes a computer extremely slow.
Flashcards
Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.
Reach the top grade on this topic
The high marks on this topic come from causal chains. A grade 9 answer explains virtual memory as a sequence of events with a consequence, not as a definition.
Weak: "Virtual memory is part of the hard drive used as RAM. It makes the computer slow."
Strong: "When RAM becomes full, the operating system moves data that has not been used recently out to the swap file on secondary storage, freeing RAM for the new program. Because secondary storage is far slower than RAM, every subsequent access to that data requires it to be swapped back in, and if this happens continuously the computer spends most of its time transferring data rather than executing instructions."
Notice the second version names the trigger, the mechanism, the location and the consequence.
- Be able to explain why ROM is needed given that RAM exists, using volatility
- Be able to describe the boot sequence in four steps
- Be able to explain the cause of slowdown when virtual memory is heavily used
- Be able to recommend adding RAM and justify why it solves the problem
Mistakes that cost marks here
- Saying ROM is 'where the operating system is stored'. The OS lives in secondary storage and is copied into RAM at boot. ROM holds only the small boot program.
- Describing volatile as 'temporary' without mentioning power. The examiner wants the link to power being removed.
- Saying virtual memory 'adds more RAM'. It does not. It uses slower secondary storage as a substitute for RAM.
- Claiming ROM can never be changed. It can be reflashed during a firmware update, it just is not written to during normal operation.
- Confusing cache with RAM. Cache is inside or beside the CPU and is much smaller and faster than RAM.
The word RAM stands for random access memory, and the random part means any location can be read in the same amount of time. Old magnetic tape was the opposite: to reach the end you had to wind through everything before it.
Knowledge check
Exam-style questions
Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.
- LRAM is volatile while ROM is non volatile
- LRAM can be read from and written to, ROM is read only in normal use
RAM is volatile, meaning its contents are lost when the power is switched off, whereas ROM is non volatile and keeps its contents. RAM can also be both read from and written to during normal operation, while ROM is read only in normal use.
- LHolds the boot program or BIOS
- LIt is non volatile so the instructions are still present when the computer is switched on
- LThe boot program checks hardware and loads the operating system from secondary storage into RAM
ROM holds the bootstrap program, sometimes called the BIOS, which is the set of instructions the computer runs the moment it is switched on. ROM is used because it is non volatile, so unlike RAM its contents survive the computer being powered down and the instructions are guaranteed to be present at start up. The boot program checks that the hardware is working, then locates the operating system in secondary storage and copies it into RAM so that the CPU can run it.
- LVirtual memory is an area of secondary storage used as if it were RAM
- LUsed when RAM is full and more space is needed
- LData not being used recently is moved from RAM out to the swap file
- LIt is swapped back into RAM when needed again
Virtual memory is an area of secondary storage, such as a region of the hard disk or SSD known as the swap file, that the operating system uses as though it were additional RAM. It is used when RAM becomes full but another program or piece of data needs to be loaded. The operating system identifies data in RAM that has not been accessed recently and moves it out to the swap file, freeing physical RAM for the new data. If the data that was moved out is needed again later, it is swapped back into RAM, which may require moving something else out to make room.
- L4 GB of RAM fills up when several applications are open
- LThe operating system starts using virtual memory on secondary storage
- LData is repeatedly swapped between RAM and secondary storage
- LSecondary storage is far slower to access than RAM
- LThe CPU spends time waiting rather than executing instructions, which the user experiences as slowdown
- LRecommends installing more RAM, so fewer programs need to be swapped out
With only 4 GB of RAM, opening several applications at once quickly fills the available physical memory. Once RAM is full the operating system falls back on virtual memory, which is an area of the laptop's secondary storage used as if it were RAM. Data belonging to programs that have not been used recently is written out to the swap file, and when the user switches back to those programs that data has to be read back into RAM again, usually forcing something else out. Because secondary storage is thousands of times slower to access than RAM, each of these swaps takes a substantial amount of time, and the CPU sits waiting for data instead of executing instructions. When it happens continuously it is known as disk thrashing, and the user experiences it as severe slowdown. The most effective solution is to install more RAM, for example upgrading to 16 GB, because all the open applications can then be held in physical memory at once and swapping is largely eliminated. Replacing a mechanical hard disk with an SSD would reduce the penalty of each swap, but it treats the symptom rather than the cause.
- LPrimary storage is directly accessible by the CPU
- LIt holds the data and instructions currently in use
- LPrograms are copied from secondary storage into primary storage before they can be executed
Primary storage is the memory that the CPU is able to access directly, and it holds the data and instructions that are currently being used, including the operating system and any programs the user has open. The CPU cannot execute a program directly from secondary storage, so when a program is opened it is first copied from secondary storage into primary storage, and the CPU then fetches its instructions from there during the fetch decode execute cycle.