Memory and storage
Units of storage, the memory hierarchy, primary versus secondary memory, RAM and ROM and their variants, cache, and the storage devices in use.
Module contents
Units
The smallest unit of storage is the bit — a binary digit, holding or . Bits are grouped into bytes.
One byte holds one character. Above that, each unit is times the last.
| Unit | Equals | Bytes |
|---|---|---|
| Kilobyte (KB) | bytes | |
| Megabyte (MB) | KB | |
| Gigabyte (GB) | MB | |
| Terabyte (TB) | GB | |
| Petabyte (PB) | TB | |
| Exabyte (EB) | PB |
A word is the number of bits the CPU handles as a unit — 32 bits or 64 bits on current machines. Word length is one measure of a processor's capability.
The memory hierarchy
Memory is arranged as a pyramid. Going down it, capacity rises and cost per byte falls, but access time gets worse.
No single technology is both fast and cheap, so a computer uses several and moves data between them. That trade-off is the whole reason the hierarchy exists.
Primary memory
Directly accessible by the CPU. Semiconductor, fast, and limited in size.
RAM — Random Access Memory
The computer's working memory. It holds the operating system, the programs currently running and the data they are using.
Two defining properties:
- Random access — any location can be reached directly, in the same time as any other. Contrast a magnetic tape, where reaching the middle means winding through everything before it.
- Volatile — the contents are lost the moment power is removed. This is why unsaved work disappears in a power cut.
Two kinds of RAM:
| SRAM (static) | DRAM (dynamic) | |
|---|---|---|
| Stores a bit in | A flip-flop (transistors) | A capacitor |
| Needs refreshing? | No | Yes, thousands of times a second |
| Speed | Faster | Slower |
| Cost and size | Expensive, bulky | Cheap, dense |
| Used as | Cache | Main memory |
ROM — Read Only Memory
Holds programs and data written once and thereafter only read. Non-volatile — contents survive power loss. ROM holds the instructions the machine needs before any software has loaded: the BIOS/UEFI and the bootstrap loader, which is the program that finds and starts the operating system. Firmware lives in ROM.
Variants of ROM:
- MROM — masked ROM; contents fixed during manufacture, unchangeable.
- PROM — programmable ROM; blank when made, written once by the user with a special device, then permanent.
- EPROM — erasable PROM; erased wholesale by exposure to ultraviolet light through a window in the chip, then rewritten.
- EEPROM — electrically erasable PROM; erased and rewritten electrically, byte by byte, without removing the chip. Flash memory is a form of EEPROM, and is what pen drives, memory cards and SSDs are built from.
RAM against ROM
| RAM | ROM | |
|---|---|---|
| Read/write | Read and write | Read only (normally) |
| Volatility | Volatile | Non-volatile |
| Holds | Programs and data in current use | BIOS, bootstrap loader, firmware |
| Capacity | Large (GB) | Small (KB–MB) |
| Speed | Faster | Slower |
| Cost per byte | Higher | Lower |
Cache memory
A small, very fast SRAM buffer between the CPU and main memory, holding the data and instructions most recently or most frequently used. When the CPU needs an item it looks in cache first:
- found — a cache hit, served at cache speed
- not found — a cache miss, fetched from RAM (and copied into cache on the way)
Cache works because of locality of reference: programs tend to use the same data, and instructions physically near each other, repeatedly. Levels L1 (smallest, inside each core), L2 and L3 (largest, shared) form a hierarchy of their own.
Virtual memory is the opposite trick: part of the hard disk is used as though it were RAM, so programs larger than physical memory can run. It works, but disk is far slower than RAM, so heavy use of virtual memory makes a machine crawl — the state called thrashing.
Secondary memory
Non-volatile, large, cheap per byte, and not directly accessible by the CPU — data must be copied into RAM before it can be used.
Magnetic
- Hard disk drive (HDD). One or more rigid platters coated with magnetic material, spinning at 5400–7200 rpm under read/write heads. Data is organised in concentric tracks, divided into sectors; the same track on every platter forms a cylinder. Access time is the sum of seek time (head moves to the track), rotational latency (the sector comes round) and transfer time. Capacity in terabytes, cheap, but mechanical and therefore fragile and comparatively slow.
- Magnetic tape. Sequential access only. Effectively obsolete for daily work, but still the cheapest medium per byte and so used for archives and backups.
Optical
Written and read by laser, in a single spiral track from the centre outward.
| CD | DVD | Blu-ray | |
|---|---|---|---|
| Capacity | 700 MB | 4.7 GB (single layer) | 25 GB (single layer) |
Suffixes: -ROM (pressed, read only), -R (recordable once), -RW (rewritable).
Solid state
No moving parts; built from flash memory (EEPROM).
- SSD — solid state drive. Far faster than an HDD, silent, shock-resistant, more expensive per byte. Now the normal system drive.
- Pen drive / USB flash drive, memory cards (SD, microSD) — small, removable, convenient.
Cloud storage
Data held on a provider's servers and reached over the Internet — Google Drive, OneDrive, Dropbox. Accessible from anywhere and backed up by the provider, but dependent on a network connection and on trusting a third party with the data.
Access methods
- Sequential access — records must be read in order from the beginning. Magnetic tape.
- Direct (random) access — any record reached without reading the others. RAM, disks, SSDs.
Worked examples
2 solvedEvery step is shown, in the order you would write it in an answer book.
Calculating storage capacity
Question
A page of text holds lines of characters. How many such pages fit on a MB CD? How many fit on a GB pen drive?
Find the size of one page
One character occupies one byte, so
Convert the CD capacity to bytes
Divide
Over a quarter of a million pages of plain text on one CD.
Repeat for the pen drive
Roughly million pages — about times the CD.
Read the result
Plain text is tiny. Storage fills up with images, audio and video, not with words — a single photograph from a phone camera occupies more space than a thousand pages of text.
Answer
About pages on a 700 MB CD and about million on a 64 GB pen drive.
Why unsaved work disappears
Question
A student types two pages in a word processor without saving, and the power fails. Explain exactly what is lost and why, in terms of the memory hierarchy.
Locate the document while it is being typed
Every keystroke is placed in RAM — main memory. That is where the running copy of the document lives, together with the word-processor program itself.
State the property of RAM that matters
RAM is volatile: it stores each bit as a charge that must be continuously refreshed by the power supply. Remove power and the charge drains within moments, taking the contents with it.
Explain what saving does
Saving copies the document from RAM to secondary memory — an SSD or hard disk. Those are non-volatile: an SSD stores each bit as trapped charge in a flash cell and a hard disk as a magnetised region, neither of which needs power to persist.
Conclude
Everything typed since the last save existed only in volatile RAM and is unrecoverable. Anything saved earlier is intact on disk. Autosave features work by silently performing this copy every few minutes.
Answer
Everything since the last save is lost, because the working copy was in volatile RAM; saving copies it to non-volatile secondary memory.
NoteThis is the standard exam illustration of volatility. Name RAM, say volatile, and say what saving actually copies.
Practice problems
6 with solutionsWork each one on paper first. The full solution — not just the answer — is one click away.
Arrange in increasing order of size: bit, terabyte, byte, kilobyte, nibble, gigabyte, megabyte. State how many bytes each of the last four contains.
Show solutionHide solution
Increasing order:
- bit binary digit ( or )
- nibble bits
- byte bits one character
- KB bytes bytes
- MB KB bytes
- GB MB bytes
- TB GB bytes
Distinguish between primary and secondary memory under at least five headings.
Show solutionHide solution
| Heading | Primary memory | Secondary memory |
|---|---|---|
| CPU access | Directly accessible by the CPU | Not direct — data must be copied into primary memory first |
| Volatility | RAM is volatile; contents lost on power off | Non-volatile; contents persist |
| Speed | Very fast (nanoseconds) | Much slower (microseconds for SSD, milliseconds for HDD) |
| Capacity | Smaller — gigabytes | Much larger — terabytes |
| Cost per byte | High | Low |
| Technology | Semiconductor | Magnetic, optical or flash |
| Purpose | Holds what is in use now | Stores data and programs long term |
| Examples | RAM, ROM, cache, registers | Hard disk, SSD, CD/DVD, pen drive, magnetic tape |
The single sentence that carries the distinction: primary memory holds what the computer is working on, secondary memory holds what it will need later.
Explain RAM and ROM, and describe the four types of ROM.
Show solutionHide solution
RAM — Random Access Memory. The computer's working memory, holding the operating system, running programs and their data. Two defining properties: any location can be accessed directly in the same time as any other (random access), and the contents are lost when power is removed (volatile). It is read/write. Two kinds:
- SRAM — stores each bit in a flip-flop, needs no refreshing, fast but expensive and bulky; used for cache.
- DRAM — stores each bit as charge in a capacitor, must be refreshed thousands of times per second, slower but cheap and dense; used for main memory.
ROM — Read Only Memory. Holds data written once and thereafter only read, and is non-volatile. It contains what the machine needs before any software is available: the BIOS/UEFI and the bootstrap loader that locates and starts the operating system.
Types of ROM.
-
MROM (Masked ROM). Contents fixed during manufacture using a mask. Cannot be changed at all. Cheapest in very large quantities.
-
PROM (Programmable ROM). Supplied blank; the user writes it once with a special device called a PROM programmer or burner, after which it is permanent. Write once, read many.
-
EPROM (Erasable PROM). Can be erased and rewritten, but only wholesale: the whole chip is cleared by exposing it to ultraviolet light through a quartz window, which takes several minutes and requires removing the chip.
-
EEPROM (Electrically Erasable PROM). Erased and rewritten electrically, byte by byte, in place, without removing the chip. Flash memory is a form of EEPROM, and is what pen drives, memory cards and SSDs are made of — which is why a modern BIOS can be updated by software.
The progression across the four is simply increasing ease of rewriting.
What is cache memory? Explain cache hit, cache miss and locality of reference, and say why cache improves performance.
Show solutionHide solution
Cache memory is a small block of very fast SRAM placed between the CPU and main memory. It holds copies of the data and instructions the CPU has used most recently or uses most often.
How a request is served. When the CPU needs an item it looks in cache first.
- Cache hit — the item is present, and is supplied at cache speed. No RAM access is needed.
- Cache miss — the item is absent, so it must be fetched from the slower main memory. On the way it is also copied into cache, on the expectation that it will be wanted again.
The hit ratio is the fraction of requests that are hits, and it is the figure that decides how much the cache is helping.
Why it works — locality of reference. Real programs do not access memory at random. They exhibit:
- temporal locality — an item used now is likely to be used again shortly (a loop counter, a running total)
- spatial locality — an item near one just used is likely to be needed next (the following instruction, the next element of an array)
Because of this, a small cache holding recent items captures a large share of all accesses.
Why performance improves. RAM access costs the CPU many idle clock cycles; cache access costs very few. If most accesses are hits, the CPU spends most of its time computing rather than waiting. Without cache, a fast processor would sit idle waiting on comparatively slow DRAM, and its speed would be largely wasted.
Levels. L1 is smallest and fastest, inside each core; L2 is larger and slightly slower; L3 is largest and shared between cores — a hierarchy within the hierarchy, for exactly the same reason as the main one.
Compare a hard disk drive with a solid state drive, and explain the terms seek time and rotational latency.
Show solutionHide solution
| Hard disk drive (HDD) | Solid state drive (SSD) | |
|---|---|---|
| Technology | Magnetic platters and moving heads | Flash memory (EEPROM) |
| Moving parts | Yes | None |
| Speed | Slower (milliseconds) | Much faster (microseconds) |
| Noise and heat | Audible, warmer | Silent, cooler |
| Shock resistance | Poor — a knock can crash the heads | Good |
| Power use | Higher | Lower |
| Cost per byte | Low | Higher |
| Lifetime limit | Mechanical wear | Limited number of write cycles per cell |
Organisation of a hard disk. Data sits in concentric tracks on each platter surface; each track is divided into sectors, the smallest addressable unit; the same numbered track on every platter together forms a cylinder.
Access time on an HDD is the sum of three parts:
- Seek time — the time for the read/write head to move radially to the correct track. Mechanical, and the largest component.
- Rotational latency — having reached the track, the time waiting for the required sector to rotate under the head. On average half a revolution: at rpm one revolution takes ms, so average latency is about ms.
- Transfer time — the time to actually read or write the data once positioned.
Why an SSD is so much faster. It has no head to move and no platter to rotate, so both seek time and rotational latency are zero. This is also why an SSD's performance barely changes when files are scattered, whereas a fragmented HDD slows noticeably.
Explain the memory hierarchy. Why not simply build a computer with a very large amount of the fastest memory?
Show solutionHide solution
The hierarchy. From fastest and smallest to slowest and largest:
Moving down the hierarchy, capacity rises and cost per byte falls; access time gets worse. Moving up, everything gets faster and more expensive per byte.
Why not build the whole machine from the fastest memory? Three reasons, and all three would still apply even with unlimited money.
1. Cost. SRAM, the memory used for cache, costs orders of magnitude more per byte than DRAM, which in turn costs far more than flash or magnetic storage. Building a machine with several terabytes of SRAM would put its price beyond any conceivable use.
2. Physics — density and heat. An SRAM cell needs six transistors; a DRAM cell needs one transistor and one capacitor. SRAM is therefore several times larger per bit, so far less of it fits on a chip. It also draws more power and produces more heat, which limits how much can be packed together and cooled.
3. Physics — distance. Even with free, cool, dense fast memory, signals travel at a finite speed. A larger memory occupies more physical space, so its far corners are further from the CPU and take longer to reach. Big memory is slow memory, unavoidably. This is why L1 cache is deliberately kept tiny — a larger L1 would be a slower L1.
Why the hierarchy works anyway. Because of locality of reference, a program at any moment uses only a small fraction of its data. The hierarchy keeps that active fraction near the top and everything else lower down, and moves items up as they are needed. The result is a system whose average access time approaches that of the fast levels while its capacity is that of the slow ones.
Volatility adds a second reason. The fast levels are all volatile. Even if a machine could hold everything in RAM, it would still need non-volatile secondary storage so that data survives being switched off.